Python制作简单的剪刀石头布游戏


Posted in Python onDecember 10, 2020

关于程序相关的

  • 您可以反复玩游戏,直到选择停止为止。
  • 该程序跟踪获胜情况。
  • 大小写无关紧要(即ROCK与Rock相同)。
  • 如果您输入的内容无效,程序会一直提示您,直到您输入有效的内容。

对项目进行编码的步骤:

  1. 创建一个简单的单轮游戏版本,我们不执行正确的输入。
  2. 如果输入了无效的内容,则添加while循环可重新提示用户输入选择。
  3. 使用while循环让用户反复播放,并使用变量来跟踪得分。

程序代码

import random

input("Welcome to Rock, Paper, Scissors! Press Enter to start.")
print()
user_wins = 0
computer_wins = 0

choices = ["rock", "paper", "scissors"]

while True:
 random_index = random.randint(0,2)
 cpu_choice = choices[random_index]

 user_choice = input("Rock, Paper, or Scissors? ").lower()
 while user_choice not in choices:
  user_choice = input("That is not a valid choice. Please try again: ").lower()
 
 print()
 print("Your choice:", user_choice)
 print("Computer's choice:", cpu_choice)
 print()

 if user_choice == 'rock':
  if cpu_choice == 'rock':
   print("It's a tie!")
  elif cpu_choice == 'scissors':
   print("You win!")
   user_wins+=1
  elif cpu_choice == 'paper':
   print("You lose!")
   computer_wins+=1
 elif user_choice == 'paper':
  if cpu_choice == 'paper':
   print("It's a tie!")
  elif cpu_choice == 'rock':
   print("You win!")
   user_wins+=1
  elif cpu_choice == 'scissors':
   print("You lose!")
   computer_wins+=1
 elif user_choice == 'scissors':
  if cpu_choice == 'scissors':
   print("It's a tie!")
  elif cpu_choice == 'paper':
   print("You win!")
   user_wins+=1
  elif cpu_choice == 'rock':
   print("You lose!")
   computer_wins+=1

 print()
 print("You have "+str(user_wins)+" wins")
 print("The computer has "+str(computer_wins)+" wins")
 print()

 repeat = input("Play again? (Y/N) ").lower()
 while repeat not in ['y', 'n']:
  repeat = input("That is not a valid choice. Please try again: ").lower()
 
 if repeat == 'n':
  break

 print("\n----------------------------\n")

运行效果:

Python制作简单的剪刀石头布游戏

以上就是Python制作简单的剪刀石头布游戏的详细内容,更多关于Python 剪刀石头布游戏的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python中使用urllib2模块编写爬虫的简单上手示例
Jan 20 Python
Python ftp上传文件
Feb 13 Python
使用Python的Scrapy框架十分钟爬取美女图
Dec 26 Python
django静态文件加载的方法
May 20 Python
python中的二维列表实例详解
Jun 19 Python
python中pip的安装与使用教程
Aug 10 Python
Tensorflow分类器项目自定义数据读入的实现
Feb 05 Python
详解Python循环作用域与闭包
Mar 21 Python
python异步实现定时任务和周期任务的方法
Jun 29 Python
django实现web接口 python3模拟Post请求方式
Nov 19 Python
python中的getter与setter你了解吗
Mar 24 Python
详解Python中的for循环
Apr 30 Python
python给list排序的简单方法
Dec 10 #Python
详解java调用python的几种用法(看这篇就够了)
Dec 10 #Python
Python利用imshow制作自定义渐变填充柱状图(colorbar)
Dec 10 #Python
详解Python GUI编程之PyQt5入门到实战
Dec 10 #Python
python 实现ping测试延迟的两种方法
Dec 10 #Python
弄清Pytorch显存的分配机制
Dec 10 #Python
python实现经纬度采样的示例代码
Dec 10 #Python
You might like
Php中文件下载功能实现超详细流程分析
2012/06/13 PHP
如何判断php数组的维度
2013/06/10 PHP
PHP数组和explode函数示例总结
2015/05/08 PHP
PHP连接SQL server数据库测试脚本运行实例
2020/08/24 PHP
jQuery 全选效果实现代码
2009/03/23 Javascript
关于jQuery中的end()使用方法
2011/07/10 Javascript
浅谈javascript的原型继承
2012/07/25 Javascript
jQuery实现向下滑出的二级菜单效果实例
2015/08/22 Javascript
学习JavaScript设计模式(链式调用)
2015/11/26 Javascript
谈谈jQuery Ajax用法详解
2015/11/27 Javascript
轻松掌握JavaScript装饰者模式
2016/08/27 Javascript
JS与jQuery实现隔行变色的方法
2016/09/09 Javascript
JS实现快速的导航下拉菜单动画效果附源码下载
2016/11/01 Javascript
node.js ws模块搭建websocket服务端的方法示例
2019/04/25 Javascript
layui layer select 选择被遮挡的解决方法
2019/09/21 Javascript
详解JavaScript中new操作符的解析和实现
2020/09/04 Javascript
python在linux系统下获取系统内存使用情况的方法
2015/05/11 Python
pyspark 读取csv文件创建DataFrame的两种方法
2018/06/07 Python
Python3日期与时间戳转换的几种方法详解
2019/06/04 Python
Python多版本开发环境管理工具介绍
2019/07/03 Python
如何使用python爬虫爬取要登陆的网站
2019/07/12 Python
python之PyQt按钮右键菜单功能的实现代码
2019/08/17 Python
keras训练曲线,混淆矩阵,CNN层输出可视化实例
2020/06/15 Python
python 密码学示例——理解哈希(Hash)算法
2020/09/21 Python
Python爬取豆瓣数据实现过程解析
2020/10/27 Python
python 监控服务器是否有人远程登录(详细思路+代码)
2020/12/18 Python
德国体育用品网上商店:SC24.com
2016/08/01 全球购物
英国领先的电子、技术和办公用品购物网站:Ebuyer
2018/04/04 全球购物
英国最大的在线床超市:Bed Star
2019/01/24 全球购物
西班牙三叶草药房:Farmacias Trébol
2019/05/03 全球购物
荣耀俄罗斯官网:HONOR俄罗斯
2020/10/31 全球购物
毕业生个人投资创业计划书
2014/01/04 职场文书
法学院方阵解说词
2014/01/29 职场文书
2015年办公室个人工作总结
2015/04/20 职场文书
Python实现提取PDF简历信息并存入Excel
2022/04/02 Python
Elasticsearch Recovery 详细介绍
2022/04/19 Java/Android