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 20 Python
Python实现曲线点抽稀算法的示例
Oct 12 Python
Python通过future处理并发问题
Oct 17 Python
Python中字典的浅拷贝与深拷贝用法实例分析
Jan 02 Python
tensorflow输出权重值和偏差的方法
Feb 10 Python
wxPython实现窗口用图片做背景
Apr 25 Python
Django1.11配合uni-app发起微信支付的实现
Oct 12 Python
python的faker库用法
Nov 28 Python
关于Python字符串显示u...的解决方式
Mar 06 Python
Django与pyecharts结合的实例代码
May 13 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
May 15 Python
Python制作春联的示例代码
Jan 22 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
星际争霸任务指南——人族
2020/03/04 星际争霸
社区(php&&mysql)一
2006/10/09 PHP
PHP Header用于页面跳转要注意的几个问题总结
2008/10/03 PHP
PHP has encountered an Access Violation at 7C94BD02解决方法
2009/08/24 PHP
浅析PHP substr,mb_substr以及mb_strcut的区别和用法
2013/06/21 PHP
PHP获取数组中单列值的方法
2017/06/10 PHP
PHP使用gearman进行异步的邮件或短信发送操作详解
2020/02/27 PHP
js各种验证文本框输入格式(正则表达式)
2010/10/22 Javascript
JavaScript实现快速排序(自已编写)
2012/12/19 Javascript
复制js对象方法(详解)
2013/07/08 Javascript
js onclick事件传参讲解
2013/11/06 Javascript
javascript实现数字验证码的简单实例
2014/02/10 Javascript
基于NodeJS的前后端分离的思考与实践(五)多终端适配
2014/09/26 NodeJs
关于JavaScript的变量的数据类型的判断方法
2015/08/14 Javascript
jquery UI Datepicker时间控件的使用方法(加强版)
2015/11/07 Javascript
Bootstrap中data-target 到底是什么
2017/02/14 Javascript
网页中的图片查看器viewjs使用方法
2017/07/11 Javascript
关于Vue项目跨平台运行问题的解决方法
2018/09/18 Javascript
微信小程序之事件交互操作实例分析
2018/12/03 Javascript
解决vue字符串换行问题(绝对管用)
2020/08/06 Javascript
python写的一个文本编辑器
2014/01/23 Python
Python操作使用MySQL数据库的实例代码
2017/05/25 Python
解决tensorflow测试模型时NotFoundError错误的问题
2018/07/27 Python
详解利用Python scipy.signal.filtfilt() 实现信号滤波
2019/06/05 Python
Python3.7将普通图片(png)转换为SVG图片格式(网站logo图标)动起来
2020/04/21 Python
浅谈TensorFlow中读取图像数据的三种方式
2020/06/30 Python
python实现KNN近邻算法
2020/12/30 Python
css3使用animation属性实现炫酷效果(推荐)
2020/02/04 HTML / CSS
全球立体声:World Wide Stereo
2018/09/29 全球购物
大学毕业生自我鉴定
2013/11/05 职场文书
DIY蛋糕店的创业计划书范文
2013/12/26 职场文书
关爱残疾人演讲稿
2014/05/24 职场文书
人事行政经理岗位职责
2014/06/18 职场文书
2015年重阳节活动总结
2015/03/24 职场文书
高中开学感言
2015/08/01 职场文书
六五普法学习心得体会
2016/01/21 职场文书