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基于windows平台锁定键盘输入的方法
Mar 05 Python
Python入门教程之if语句的用法
May 14 Python
Python基于pillow判断图片完整性的方法
Sep 18 Python
Python写的一个定时重跑获取数据库数据
Dec 28 Python
Python+Pandas 获取数据库并加入DataFrame的实例
Jul 25 Python
对python实现合并两个排序链表的方法详解
Jan 23 Python
Python中单线程、多线程和多进程的效率对比实验实例
May 14 Python
简单了解Python3里的一些新特性
Jul 13 Python
nginx+uwsgi+django环境搭建的方法步骤
Nov 25 Python
python GUI库图形界面开发之PyQt5日期时间控件QDateTimeEdit详细使用方法与实例
Feb 27 Python
Python语法垃圾回收机制原理解析
Mar 25 Python
pip安装tensorflow的坑的解决
Apr 19 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
轻松入门: 煮好咖啡的七个诀窍
2021/03/03 冲泡冲煮
让你同时上传 1000 个文件 (二)
2006/10/09 PHP
mysql,mysqli,PDO的各自不同介绍
2012/09/19 PHP
基于php常用正则表达式的整理汇总
2013/06/08 PHP
浅析php fwrite写入txt文件的时候用 \r\n不能换行的问题
2013/08/06 PHP
PHP header()函数常用方法总结
2014/04/11 PHP
PHP读取RSS(Feed)简单实例
2014/06/12 PHP
destoon实现调用自增数字从1开始的方法
2014/08/21 PHP
php里array_work用法实例分析
2015/07/13 PHP
以实例全面讲解PHP中多进程编程的相关函数的使用
2015/08/18 PHP
学习ExtJS accordion布局
2009/10/08 Javascript
JS跨域代码片段
2012/08/30 Javascript
实现动画效果核心方式的js代码
2013/09/27 Javascript
javaScript中的this示例学习详解及工作原理
2014/01/13 Javascript
javascript实现点击按钮让DIV层弹性移动的方法
2015/02/24 Javascript
jQuery中使用each处理json数据
2015/04/23 Javascript
JS实现样式清新的横排下拉菜单效果
2015/10/09 Javascript
jQuery实现鼠标滚动图片延迟加载效果附源码下载
2016/06/28 Javascript
Vue隐藏显示、只读实例代码
2018/07/18 Javascript
详解几十行代码实现一个vue的状态管理
2019/01/28 Javascript
vue使用websocket的方法实例分析
2019/06/22 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
Python安装第三方库及常见问题处理方法汇总
2016/09/13 Python
对Python中实现两个数的值交换的集中方法详解
2019/01/11 Python
Python对Tornado请求与响应的数据处理
2020/02/12 Python
windows、linux下打包Python3程序详细方法
2020/03/17 Python
全球度假村:Club Med
2017/11/27 全球购物
巴西Mr. Cat在线商店:购买包包和鞋子
2019/09/08 全球购物
华为慧通面试题
2012/09/11 面试题
Prototype中如何为一个元素添加一个方法
2014/12/08 面试题
优秀家长事迹材料
2014/05/17 职场文书
国家领导干部党的群众路线教育实践活动批评与自我批评材料
2014/09/23 职场文书
走群众路线剖析材料
2014/10/09 职场文书
2015年小学实验室工作总结
2015/07/28 职场文书
2019思想汇报范文
2019/05/21 职场文书
漫画「你在春天醒来」第10卷封面公开
2022/03/21 日漫