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基于twisted实现简单的web服务器
Sep 29 Python
使用Python编写vim插件的简单示例
Apr 17 Python
python基础教程之五种数据类型详解
Jan 12 Python
Python中单、双下划线的区别总结
Dec 01 Python
Python实现调用另一个路径下py文件中的函数方法总结
Jun 07 Python
python调用摄像头显示图像的实例
Aug 03 Python
对Python协程之异步同步的区别详解
Feb 19 Python
Python实现将蓝底照片转化为白底照片功能完整实例
Dec 13 Python
Keras设定GPU使用内存大小方式(Tensorflow backend)
May 22 Python
浅谈优化Django ORM中的性能问题
Jul 09 Python
Scrapy-Redis之RedisSpider与RedisCrawlSpider详解
Nov 18 Python
SpringBoot首页设置解析(推荐)
Feb 11 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学习笔记 面向对象中[接口]与[多态性]的应用
2011/06/16 PHP
php实现singleton()单例模式实例
2014/11/06 PHP
php数组合并与拆分实例分析
2015/06/12 PHP
ThinkPHP框架安全实现分析
2016/03/14 PHP
用 JSON 处理缓存
2007/04/27 Javascript
JavaScript 动态将数字金额转化为中文大写金额
2009/05/14 Javascript
javascript 多浏览器 事件大全
2010/03/23 Javascript
javascript拓展DOM操作 prependChild insertAfert
2010/11/17 Javascript
js作用域及作用域链概念理解及使用
2013/04/15 Javascript
window.location.href = window.location.href 跳转无反应 a超链接onclick事件写法
2013/08/21 Javascript
JavaScript实现仿新浪微博大厅和腾讯微博首页滚动特效源码
2015/09/15 Javascript
HTML Table 空白单元格补全的简单实现
2016/10/13 Javascript
基于vue+ bootstrap实现图片上传图片展示功能
2017/05/17 Javascript
详解angularJS动态生成的页面中ng-click无效解决办法
2017/06/19 Javascript
详解javascript 正则表达式之分组与前瞻匹配
2018/05/30 Javascript
对vue2.0中.vue文件页面跳转之.$router.push的用法详解
2018/08/24 Javascript
vue中当图片地址无效的时候,显示默认图片的方法
2018/09/18 Javascript
vue中利用Promise封装jsonp并调取数据
2019/06/18 Javascript
node中实现删除目录的几种方法
2019/06/24 Javascript
JS实现小星星特效
2019/12/24 Javascript
解决Antd Table表头加Icon和气泡提示的坑
2020/11/17 Javascript
Python中格式化format()方法详解
2017/04/01 Python
分析Python中解析构建数据知识
2018/01/20 Python
python面试题小结附答案实例代码
2019/04/11 Python
python学习开发mock接口
2019/04/28 Python
24式加速你的Python(小结)
2019/06/13 Python
国际知名设计师时装商店:Coggles
2016/09/05 全球购物
微软台湾官方网站:Microsoft台湾
2018/08/15 全球购物
Stio官网:男女、儿童户外服装
2019/12/13 全球购物
建龙钢铁面试总结
2014/04/15 面试题
大学生学习党课思想汇报
2014/01/03 职场文书
《大作家的小老师》教学反思
2014/04/16 职场文书
客户答谢会活动方案
2014/08/31 职场文书
《秋天的图画》教学反思
2016/02/19 职场文书
离婚协议书范本(2016最新版)
2016/03/18 职场文书
Linux安装Nginx步骤详解
2021/03/31 Servers