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编写web API的教程
Apr 30 Python
Python检测生僻字的实现方法
Oct 23 Python
详解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
Jun 23 Python
Python 实现引用其他.py文件中的类和类的方法
Apr 29 Python
解读python如何实现决策树算法
Oct 11 Python
Python中查看变量的类型内存地址所占字节的大小
Jun 26 Python
Python:Numpy 求平均向量的实例
Jun 29 Python
python 进程的几种创建方式详解
Aug 29 Python
docker-py 用Python调用Docker接口的方法
Aug 30 Python
基于Pytorch SSD模型分析
Feb 18 Python
python算的上脚本语言吗
Jun 22 Python
python实现高效的遗传算法
Apr 07 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中的常用函数回顾
2013/07/11 PHP
为百度UE编辑器上传图片添加水印功能
2015/04/16 PHP
javascript js cookie的存储,获取和删除
2007/12/29 Javascript
基于jquery的$.ajax async使用
2011/10/19 Javascript
JS取文本框中最小值的简单实例
2013/11/29 Javascript
jQuery实现气球弹出框式的侧边导航菜单效果
2015/09/22 Javascript
Vue.js学习之计算属性
2017/01/22 Javascript
基于canvas的二维码邀请函生成插件
2017/02/14 Javascript
js实现带三角符的手风琴效果
2017/03/01 Javascript
vue中v-for循环给标签属性赋值的方法
2018/10/18 Javascript
详解微信小程序实现跑马灯效果(附完整代码)
2019/04/29 Javascript
vue-cli3+typescript新建一个项目的思路分析
2019/08/06 Javascript
JS实现提示效果弹出及延迟隐藏的功能
2019/08/26 Javascript
JavaScript(js)处理的HTML事件、键盘事件、鼠标事件简单示例
2019/11/19 Javascript
Vue实现指令式动态追加小球动画组件的步骤
2020/12/18 Vue.js
python调用短信猫控件实现发短信功能实例
2014/07/04 Python
Python实现的手机号归属地相关信息查询功能示例
2017/06/08 Python
Python基于OpenCV实现视频的人脸检测
2018/01/23 Python
Python使用requests及BeautifulSoup构建爬虫实例代码
2018/01/24 Python
Python实现的将文件每一列写入列表功能示例【测试可用】
2018/03/19 Python
python多维数组切片方法
2018/04/13 Python
python中的句柄操作的方法示例
2019/06/20 Python
Django项目主urls导入应用中views的红线问题解决
2019/08/10 Python
如何理解python面向对象编程
2020/06/01 Python
Python中Selenium模块的使用详解
2020/10/09 Python
基于Python实现全自动下载抖音视频
2020/11/06 Python
ghd澳大利亚官方网站:英国最受欢迎的美发工具品牌
2018/05/21 全球购物
建筑工程技术应届生求职信
2013/11/17 职场文书
工作疏忽检讨书
2014/01/25 职场文书
户外拓展活动方案
2014/02/11 职场文书
大学学生会竞选演讲稿
2014/04/25 职场文书
2015年学校管理工作总结
2015/07/20 职场文书
《搭石》教学反思
2016/02/18 职场文书
入伍志愿书怎么写?
2019/07/19 职场文书
PostgreSQL存储过程实用脚本(二):创建函数入门
2021/04/05 PostgreSQL
数据库之SQL技巧整理案例
2021/07/07 SQL Server