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的Django框架中使用通用视图的方法
Jul 21 Python
Python实现批量读取word中表格信息的方法
Jul 30 Python
Python优化技巧之利用ctypes提高执行速度
Sep 11 Python
windows系统下Python环境搭建教程
Mar 28 Python
Python中用字符串调用函数或方法示例代码
Aug 04 Python
Python二进制串转换为通用字符串的方法
Jul 23 Python
基于python实现学生管理系统
Oct 17 Python
pyshp创建shp点文件的方法
Dec 31 Python
django 中的聚合函数,分组函数,F 查询,Q查询
Jul 25 Python
Python loguru日志库之高效输出控制台日志和日志记录
Mar 07 Python
Flask缓存静态文件的具体方法
Aug 02 Python
关于python3 opencv 图像二值化的问题(cv2.adaptiveThreshold函数)
Apr 04 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
基于mysql的论坛(6)
2006/10/09 PHP
将文件夹压缩成zip文件的php代码
2009/12/14 PHP
php中file_exists函数使用详解
2015/05/08 PHP
php正则判断是否为合法身份证号的方法
2017/03/16 PHP
Nigma vs AM BO3 第一场2.13
2021/03/10 DOTA
关于__defineGetter__ 和__defineSetter__的说明
2007/05/12 Javascript
JQuery中html()方法使用不当带来的陷阱
2011/04/07 Javascript
定时器(setTimeout/setInterval)调用带参函数失效解决方法
2013/03/26 Javascript
JS弹出层单纯的绝对定位居中示例代码
2014/02/18 Javascript
jQuery实现简单网页遮罩层/弹出层效果兼容IE6、IE7
2014/06/16 Javascript
jQuery中的pushStack实现原理和应用实例
2015/02/03 Javascript
javascript小数精度丢失的完美解决方法
2016/05/31 Javascript
Vue.js每天必学之过滤器与自定义过滤器
2016/09/07 Javascript
Chrome不支持showModalDialog模态对话框和无法返回returnValue问题的解决方法
2016/10/30 Javascript
使用 Node.js 模拟滑动拼图验证码操作的示例代码
2017/11/02 Javascript
Vue 中使用 CSS Modules优雅方法
2018/04/09 Javascript
React Component存在的几种形式详解
2018/11/06 Javascript
微信小程序实现跳转的几种方式总结(推荐)
2019/04/24 Javascript
微信小程序页面间传值与页面取值操作实例分析
2019/04/30 Javascript
JavaScript使用localStorage存储数据
2019/09/25 Javascript
[48:41]VP vs VG Supermajor小组赛 B组胜者组决赛 BO3 第二场 6.2
2018/06/03 DOTA
py2exe 编译ico图标的代码
2013/03/08 Python
python实现BackPropagation算法
2017/12/14 Python
利用arcgis的python读取要素的X,Y方法
2018/12/22 Python
如何利用Python分析出微信朋友男女统计图
2019/01/25 Python
python3.6下Numpy库下载与安装图文教程
2019/04/02 Python
机器学习实战之knn算法pandas
2019/06/22 Python
Python定时发送天气预报邮件代码实例
2019/09/09 Python
python中pdb模块实例用法
2021/01/15 Python
欧舒丹英国官网:购买欧舒丹护手霜等明星产品
2017/01/17 全球购物
CPB肌肤之钥美国官网:Clé de Peau Beauté
2017/09/05 全球购物
考试退步检讨书
2014/01/15 职场文书
复兴之路展览观后感
2015/06/02 职场文书
师范生见习自我总结
2015/06/23 职场文书
大学生暑期实践报告之企业经营管理
2019/08/08 职场文书
毕业生自荐求职信书写的技巧
2019/08/26 职场文书