
我写的代码如图所示,目前我有以下疑问:在一盘游戏结束后要进行下一盘,是不是该重写一个while?我红色标出的部分会导致count次数增加,这个该怎么解决?
import random
print("欢迎来到猜字游戏!")
print("输入一个数字!")
count = 0
your_answer = -1
a = random.randint(0, 20)
print(a)
while your_answer != a:
your_answer = input("请输入你猜想的数字:")
if your_answer.lower() == 'q':
break
try:
if int(your_answer)<a:
print("太小了")
elif a<int(your_answer):
print("太大了!")
else:
count += 1
print(a)
print("恭喜猜对了!您一共猜了{}次".format(str(count)))
chioce = input("请输入yes或no来决定是否继续游戏?")
if chioce == 'yes':
count = 0
a = random.randint(0, 20)
continue
elif chioce == "no":
print("游戏结束!")
break
else:
print("输入有误")
count += 1
except ValueError:
print("您输入的数字不在范围内!请重新输入!")