求助教程里面文字小游戏的修改
因为学到了continue这个语句,所以今晚想试着修改教程里面的文字小游戏,但是一直都不成功,所以求助一下各位修改的内容如下:
用户在输入三次以后,都没有猜对随机生成的正确答案,在打印游戏结束的时候,是否能够让用户选择一个输入yes,或者no来继续游戏,或者退出游戏。
萌新小白前来求助,希望各位能给予指点,谢谢啦*^_^* 你可以贴一下源代码,然后方便大家修改
本帖最后由 倒戈卸甲 于 2020-4-23 01:51 编辑
你这个要求应该不在continue的业务范围内,我觉得你需要一个while循环来安放你的整个程序。
while(true):
{
你之前的整个程序
choice=temp("是否继续?")
if(choice==“no”):
return
} Zmynx_2017 发表于 2020-4-23 00:37
你可以贴一下源代码,然后方便大家修改
不好意思是我疏忽了,没注意这个问题,下次有问题会先把源代码给出来:
print("""___文字小游戏___""")
import random
secret = random.randint(1,10)
temp = input("要不要猜一下1—10之间的数字,哪个是我现在心里想的数字?:")
times = 1
while (guess != secret) and (times < 5):
if guess > secret:
print("咿呀,你猜的太大啦~~~")
else:
print("哎呀,你猜的太小啦~~~")
temp = input("再试一试吧:")
guess= int(temp)
times = times + 1
if (times <= 5) and (guess == secret):
print("你是我肚子里的蛔虫吗?!")
print("嘿嘿,猜中了呀,不过没有奖励哟!")
else:
print("唔,给了三次机会都猜错,不跟你玩了!")
倒戈卸甲 发表于 2020-4-23 01:17
你这个要求应该不在continue的业务范围内,我觉得你需要一个while循环来安放你的整个程序。
while(true):
...
不好意思,我是新人小白,发帖的时候疏忽了,没有附上源代码,麻烦您再看一下源代码,指教一下应该怎么修改:
print("""___文字小游戏___""")
import random
secret = random.randint(1,10)
temp = input("要不要猜一下1—10之间的数字,哪个是我现在心里想的数字?:")
times = 1
while (guess != secret) and (times < 5):
if guess > secret:
print("咿呀,你猜的太大啦~~~")
else:
print("哎呀,你猜的太小啦~~~")
temp = input("再试一试吧:")
guess= int(temp)
times = times + 1
if (times <= 5) and (guess == secret):
print("你是我肚子里的蛔虫吗?!")
print("嘿嘿,猜中了呀,不过没有奖励哟!")
else:
print("唔,给了三次机会都猜错,不跟你玩了!") 在你的程序的基础上帮你修改了一下
import random
print("""___文字小游戏___""")
secret =random.randint(1,10)
print("要不要猜一下1—10之间的数字,哪个是我现在心里想的数字?:")
times = 1
while True:
guess = int(input())
while (guess != secret) and (times < 3):
if guess > secret:
print("咿呀,你猜的太大啦~~~")
else:
print("哎呀,你猜的太小啦~~~")
temp = input("再试一试吧:")
guess= int(temp)
times = times + 1
if guess == secret:
break
else:
temp = input("还是不对哟,还继续玩吗(yes/no):")
if temp == 'yes':
times = 1
continue
else:
break
if (times <= 3) and (guess == secret):
print("你是我肚子里的蛔虫吗?!")
print("嘿嘿,猜中了呀,不过没有奖励哟!")
else:
print("唔,给了三次机会都猜错,不跟你玩了!") print("""___文字小游戏___""")
import random
myTrue = True
while myTrue:
secret = str(random.randint(1, 10))
times = 1
guess = input("要不要猜一下1—10之间的数字,哪个是我现在心里想的数字?:")
while times < 5:
if guess.lower() == 'n':# 输入N 或 n 直接退出。
myTrue = False
break
if guess == secret:
print("你是我肚子里的蛔虫吗?!")
print("嘿嘿,猜中了呀,不过没有奖励哟!")
break
elif guess > secret:
print("咿呀,你猜的太大啦~~~再猜:")
else:
print("哎呀,你猜的太小啦~~~")
if times>= 3 and guess != secret:
print("唔,给了三次机会都猜错,不跟你玩了!")
break
times = times + 1
guess = input()
print('Byebye!!!')
sunrise085 发表于 2020-4-23 11:13
在你的程序的基础上帮你修改了一下
谢谢{:10_297:} xiangjianshinan 发表于 2020-4-23 11:40
谢谢您的回复{:10_297:} 奔跑的陀螺 发表于 2020-4-23 16:21
谢谢您的回复
客气,大家一起加油!!!
页:
[1]