python如何询问用户是否继续游戏_Python猜猜游戏如何再现
我想指出你的代码有一些问题。
最主要的一点是,当你输入yes时,游戏不会再次运行。它所要做的就是运行main(),它将打印your game,然后询问您是否要重试一次。如果你把你的游戏放在一个定义里,你可以在必要的时候调用它。
而且,我不知道是不是只有我一个人,但如果你猜对了数字,它仍然会让你猜一个数字。您需要通过将play_again()方法放入else块来退出循环。
下面是代码。为了优化,我把它稍微修饰了一下。在import random #imports random number function
def main():
print("Welcome to the number guessing game!")
game = "your game"
print(game)
run_game()
play_again()
def run_game():
counter = 1
number = random.randint(1, 10)
while counter > 0 and counter <= 5:
guess=int(input("Try and guess the number\n"))#tells the user to try and guess the number
if guess!=number and guess > number:
print("wrong number! Try again you are too high")#tells you that you were wrong and that that you were too high
counter=counter+1#adds 1 count for every attempt it took you to guess the number
elif guess != number and guess < number:
print("wrong number! Try again you are too low!")#tells you that you were wrong and tells you that you were too low
counter=counter+1#adds 1 count for every attempt it took you to guess the number
else:
print("Well done! You have guessed the number i was thinking of! The number was " + str(number))#Prints this out when you guessed the number
print("it took you " + str(counter) + " attempts!")#tells you how many attempts it took you to guess the number
play_again()
if counter == 2:
print("4 attempts left before program ends")
if counter == 3:
print("3 attempts left before program ends")
if counter == 4:
print("2 attempts left before program ends")
if counter == 5:
print("1 attempts left before program ends")
def play_again():
while True:
retry = input("Would you like to play again?(yes or no) : ")
if retry == "yes":
main()
if retry == "no":
exit()
else:
print("I'm sorry I could not recognize what you entered")
main()
python如何询问用户是否继续游戏_Python猜猜游戏如何再现相关推荐
- pythonchallenge闯关游戏_Python Challenge游戏攻略(一)
Python Challenge是一个古老的网页闯关游戏,为程序员设计,一共33关,只有在一关通过之后,才能获得会提供下一关挑战的网页地址,以此类推到最终关.蛮有意思的,自己做了13道,后面就没坚持下 ...
- python练手小游戏_Python小游戏练手EMS员工项目自学者练习
Python小游戏练手EMS员工项目自学者练习 # 显示欢迎信息 from typing import Any, Union print('-' * 20, '欢迎使用员工管理系统', '-' * 2 ...
- python创建提示用户输入查询条件_python流程控制练习
''' if条件 ''' 示例 sex= 'female' age=19 is_beautiful=True is_successful=True height=1.70 if sex =='fema ...
- python随机猜数字游戏_Python小游戏——猜数字教程(random库教程)
今天来开发一个简单的数字逻辑游戏,猜数字(数字炸弹) 首先开发游戏第一件事,了解需求. 猜数字游戏规则: 计算机随机生成一个指定范围的数字,由玩家来猜测, 之后计算机会根据玩家提供数字来与自己生成的数 ...
- python 菜单页面、选择游戏_python菜单游戏菜单游戏。。。
我正在用pygame和其他一些库用python制作一个游戏.我把主菜单作为一个python文件,当一个玩家被选中时,它启动游戏文件.如果你在玩游戏时选择返回菜单,它会再次启动菜单.但是从新菜单我不能再 ...
- python剪刀石头布小游戏_python小游戏(剪刀石头布)
- coding: utf8-- import random #导入随机数的模块 computer = random.choice(['石头', '剪刀', '布']) player = raw_in ...
- python做的大型游戏_Python有做大型游戏的潜力吗?
由于最近我要考试忙不过来没修改回答,谢谢评论区大们的指正,我认识到的pygame库确实可以做游戏,但是pygame限制很多,特别是做大型游戏方面,3D还没有相关的优化方法等(3D游戏,pygame表示 ...
- python使用什么函数接收用户输入的数据_python 3.x:调用和执行函数的用户输入...
有许多类似的问题,但没有一个答案解决了我的问题. 我定义了几个解析大型数据集的函数.首先,我调用数据,然后将数据(在.txt中表示为行和列)组织到列表中,我将为各个数据条目编制索引.在此之后,我建立了 ...
- Java黑皮书课后题第7章:***7.35(游戏:猜字词游戏)编写一个猜字词游戏,随机产生一个单词,当用户猜测正确后,正确的字母显示出来。当用户猜出一个单词,显示猜错的次数,并询问用户是否继续猜测
***7.35(游戏:猜字词游戏)编写一个猜字词游戏,随机产生一个单词,当用户猜测正确后,正确的字母显示出来.当用户猜出一个单词,显示猜错的次数,并询问用户是否继续猜测 题目 题目描述与运行示例 破题 ...
最新文章
- 基于corosync+pacemaker实现主从高可用集群
- 体感Kinect手势识别开发基本原理
- 权限申请_Android 开发工程师必须掌握的动态权限申请,三步轻松搞定!
- git 命令行忽略提交_git命令入门(本地仓库)
- JS学习总结(9)——String
- mysql 键 索引_五、MySQL索引和键
- 整人小程序【转自CSDN】
- Linux版MySQL下载教程
- hdu 5053 the Sum of Cube(水)
- mac用什么软件测试硬盘好坏,8款最好的免费Mac工具,检测和修复常见macOS问题
- 要留住优秀员工,除了钱,你还可以用什么?
- 微信html5活动页面制作,完整的微信H5活动页面设计规范
- wifi共享大师,去除弹窗广告。
- pitfall override private method
- c语言做绘图软件,大佬们,小菜鸟想问一问用vc编译器做简易画图软件
- 支持3d android 模拟器,3D太空模拟器
- uniapp手机验证码功能实现
- Android高德地图使用自定义指南针
- 周长最短面积最大_图说、故事趣说“周长一定时,何种封闭的平面图形面积最大?”...
- Matlab 图片转gif