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递归计算N!的方法
May 05 Python
python制作爬虫并将抓取结果保存到excel中
Apr 06 Python
Win7下搭建python开发环境图文教程(安装Python、pip、解释器)
May 17 Python
python常见的格式化输出小结
Dec 15 Python
python实现八大排序算法(1)
Sep 14 Python
Python实现带参数与不带参数的多重继承示例
Jan 30 Python
一条命令解决mac版本python IDLE不能输入中文问题
May 15 Python
Python简单爬虫导出CSV文件的实例讲解
Jul 06 Python
详解Python Opencv和PIL读取图像文件的差别
Dec 27 Python
Win10下安装并使用tensorflow-gpu1.8.0+python3.6全过程分析(显卡MX250+CUDA9.0+cudnn)
Feb 17 Python
Python结合百度语音识别实现实时翻译软件的实现
Jan 18 Python
教你怎么用Python操作MySql数据库
May 31 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/02/05 PHP
初识PHP
2014/09/28 PHP
为百度UE编辑器上传图片添加水印功能
2015/04/16 PHP
win7 wamp 64位 php环境开启curl服务遇到的问题及解决方法
2018/09/16 PHP
jQuery 选择表格(table)里的行和列及改变简单样式
2012/12/15 Javascript
javascript 使用 NodeList需要注意的问题
2013/03/04 Javascript
原生javascript实现获取指定元素下所有后代元素的方法
2014/10/28 Javascript
node.js中的url.parse方法使用说明
2014/12/10 Javascript
javascript框架设计之浏览器的嗅探和特征侦测
2015/06/23 Javascript
JS+CSS3模拟溢出滚动效果
2016/08/12 Javascript
vue.js 表格分页ajax 异步加载数据
2016/10/18 Javascript
vue里面父组件修改子组件样式的方法
2018/02/03 Javascript
基于vue实现移动端圆形旋钮插件效果
2018/11/28 Javascript
layui动态加载多表头的实例
2019/09/05 Javascript
JS实现拖拽元素时与另一元素碰撞检测
2020/08/27 Javascript
python中reduce()函数的使用方法示例
2017/09/29 Python
Python实现字典的遍历与排序功能示例
2017/12/23 Python
pip matplotlib报错equired packages can not be built解决
2018/01/06 Python
Python中实例化class的执行顺序示例详解
2018/10/14 Python
python操作日志的封装方法(两种方法)
2019/05/23 Python
通过 Python 和 OpenCV 实现目标数量监控
2020/01/05 Python
TENSORFLOW变量作用域(VARIABLE SCOPE)
2020/01/10 Python
css3选择器基本介绍
2014/12/15 HTML / CSS
测试时代收集的软件测试面试题
2013/09/25 面试题
几道Java和数据库的面试题
2013/05/30 面试题
Java程序员面试题
2016/09/27 面试题
大学生毕业自我评价范文分享
2013/11/11 职场文书
幼儿园英语教学反思
2014/01/30 职场文书
中学生自我鉴定
2014/02/04 职场文书
小学英语课后反思
2014/04/26 职场文书
食品安全汇报材料
2014/08/18 职场文书
师德先进个人事迹材料
2014/12/19 职场文书
2015年优质护理服务工作总结
2015/04/08 职场文书
离婚案件被告代理词
2015/05/23 职场文书
防震减灾主题班会
2015/08/14 职场文书
Python开发工具Pycharm的安装以及使用步骤总结
2021/06/24 Python