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 命令行非阻塞输入的小例子
Sep 27 Python
Python实现向服务器请求压缩数据及解压缩数据的方法示例
Jun 09 Python
Python实现一个服务器监听多个客户端请求
Apr 12 Python
如何利用Boost.Python实现Python C/C++混合编程详解
Nov 08 Python
Python multiprocessing多进程原理与应用示例
Feb 28 Python
Python人工智能之路 之PyAudio 实现录音 自动化交互实现问答
Aug 13 Python
PHP统计代码行数的小代码
Sep 19 Python
基于python实现学生信息管理系统
Nov 22 Python
Pytorch 实现focal_loss 多类别和二分类示例
Jan 14 Python
Python urllib2运行过程原理解析
Jun 04 Python
PyQt实现计数器的方法示例
Jan 18 Python
Python中对象的比较操作==和is区别详析
Feb 12 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二分查找二种实现示例
2014/03/12 PHP
PHP防盗链代码实例
2014/08/27 PHP
php去除html标记的原生函数详解
2015/01/27 PHP
round robin权重轮循算法php实现代码
2016/05/28 PHP
php使用curl实现ftp文件下载功能
2017/05/16 PHP
PHP实现文件上传功能实例代码
2017/05/18 PHP
PHP实现基于栈的后缀表达式求值功能
2017/11/10 PHP
laravel入门知识点整理
2020/09/15 PHP
Dojo之路:如何利用Dojo实现Drag and Drop效果
2007/04/10 Javascript
用javascript实现兼容IE7的类库 IE7_0_9.zip提供下载
2007/08/08 Javascript
图片上传即时显示缩略图的js代码
2009/05/27 Javascript
js客户端快捷键管理类的较完整实现和应用
2010/06/08 Javascript
js函数参数设置默认值的一种变通实现方法
2014/05/26 Javascript
JS递归遍历对象获得Value值方法技巧
2016/06/14 Javascript
Angular企业级开发——MVC之控制器详解
2017/02/20 Javascript
Javascript 一些需要注意的细节(必看篇)
2017/07/08 Javascript
JavaScript多态与封装实例分析
2018/07/27 Javascript
浅谈VUE防抖与节流的最佳解决方案(函数式组件)
2019/05/22 Javascript
JavaScript中的this基本问题实例小结
2020/03/09 Javascript
JS严格模式原理与用法实例分析
2020/04/27 Javascript
vue.js页面加载执行created,mounted的先后顺序说明
2020/11/07 Javascript
在Angular项目使用socket.io实现通信的方法
2021/01/05 Javascript
[02:10]2018DOTA2亚洲邀请赛赛前采访-Liquid
2018/04/03 DOTA
在Docker上部署Python的Flask框架的教程
2015/04/08 Python
python字符串str和字节数组相互转化方法
2017/03/18 Python
使用Python写CUDA程序的方法
2017/03/27 Python
python中的字典操作及字典函数
2018/01/03 Python
Php多进程实现代码
2018/05/07 Python
Python数据可视化:泊松分布详解
2019/12/07 Python
Python如何将装饰器定义为类
2020/07/30 Python
微软俄罗斯官方网站:Microsoft俄罗斯
2016/09/18 全球购物
zooplus德国:便宜地订购动物用品、动物饲料、动物食品
2020/05/06 全球购物
工作中的自我评价如何写好
2013/10/28 职场文书
现金出纳岗位职责
2014/03/15 职场文书
诚信考试承诺书
2014/03/27 职场文书
出国留学单位推荐信
2015/03/26 职场文书