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快速排序代码实例
Nov 21 Python
Python 异常处理实例详解
Mar 12 Python
Python3访问并下载网页内容的方法
Jul 28 Python
python解决pandas处理缺失值为空字符串的问题
Apr 08 Python
解决已经安装requests,却依然提示No module named requests问题
May 18 Python
Python基于多线程操作数据库相关问题分析
Jul 11 Python
python实现画循环圆
Nov 23 Python
python 实现线程之间的通信示例
Feb 14 Python
python3.8.1+selenium实现登录滑块验证功能
May 22 Python
python实现将中文日期转换为数字日期
Jul 14 Python
自定义Django_rest_framework_jwt登陆错误返回的解决
Oct 18 Python
Django中如何用xlwt生成表格的方法步骤
Jan 31 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+AJAX实现无刷新注册(带用户名实时检测)
2006/12/02 PHP
Uchome1.2 1.5 代码学习 common.php
2009/04/24 PHP
PHP 输出简单动态WAP页面
2009/06/09 PHP
用php实现百度网盘图片直链的代码分享
2012/11/01 PHP
PHP向浏览器输出内容的4个函数总结
2014/11/17 PHP
thinkphp中ajax与php响应过程详解
2014/12/08 PHP
谈谈 PHP7新增功能
2015/12/16 PHP
PHP面向对象程序设计之类与反射API详解
2016/12/02 PHP
PHP创建单例后台进程的方法示例
2017/05/23 PHP
PHP实现数组向任意位置插入,删除,替换数据操作示例
2019/04/05 PHP
laravel配置Redis多个库的实现方法
2019/04/10 PHP
用javascript实现计算两个日期的间隔天数
2007/08/14 Javascript
JQuery 无废话系列教程(二) jquery实战篇上
2009/06/23 Javascript
JS 实现导航栏悬停效果(续2)
2013/09/24 Javascript
Jquery 类网页微信二维码图块滚动效果具体实现
2013/10/14 Javascript
js定时器的使用(实例讲解)
2014/01/06 Javascript
JQuery复制DOM节点的方法
2015/06/11 Javascript
修改js confirm alert 提示框文字的简单实例
2016/06/10 Javascript
React快速入门教程
2017/01/17 Javascript
JavaScript中的return布尔值的用法和原理解析
2017/08/14 Javascript
浅谈react-router HashRouter和BrowserRouter的使用
2017/12/29 Javascript
安装vue-cli的简易过程
2018/05/22 Javascript
加速vue组件渲染之性能优化
2020/04/09 Javascript
Python基于递归算法实现的走迷宫问题
2017/08/04 Python
如何在python中使用selenium的示例
2017/12/26 Python
python实现任意位置文件分割的实例
2018/12/14 Python
Python实现钉钉发送报警消息的方法
2019/02/20 Python
Python程序暂停的正常处理方法
2019/11/07 Python
Python 排序最长英文单词链(列表中前一个单词末字母是下一个单词的首字母)
2020/12/14 Python
英国在线自行车商店:Evans Cycles
2016/09/26 全球购物
大学毕业生个人自荐书
2014/07/02 职场文书
党的群众路线教育实践活动实施方案
2014/10/31 职场文书
演讲比赛主持词
2015/06/29 职场文书
运输公司工作总结
2015/08/11 职场文书
2016年清明节网上祭英烈活动总结
2016/04/01 职场文书
Python 语言实现六大查找算法
2021/06/30 Python