|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
要求从1-10中随机选一个数字,然后每次猜一个,每次猜完会告诉你大了还是小了,只能猜五次,第五次输入数字之后如果没猜中,会告诉你猜错了。但是我的程序为什么第五次输入错误之后还会继续显示:你猜的数字大了或者小了,然后再告诉你猜错了。求大神帮助,感激不尽!!
这是我的程序:
import random
counts = 1
turns = 5
answer = random.randint(1,10)
print("Pick a number between 1 and 10.\nYou will have 5 turns.")
while counts <= 5:
temp = input("Your guess is \n")
guess = int(temp)
turns = turns - 1
if guess == answer:
print("It took you",counts,"turns to guess my number, which was",answer,".")
break
else:
if guess < answer:
print("Turns ",counts+1)
print("Your guess,",guess,",is too low")
print("Pick a number between 1 and 10.")
print("You have ", turns, " turns left.")
else:
print("Turns ",counts+1)
print("Your guess,",guess,",is too high")
print("Pick a number between 1 and 10.")
print("You have ", turns, " turns left.")
counts = counts + 1
else:
print("Oops!!No turns left.My number was",answer,".")
你想要的是这样的效果? counts = 1
turns = 5
answer = random.randint(1, 10)
print("Pick a number between 1 and 10.\nYou will have 5 turns.")
while counts <= 5:
temp = input("Your guess is \n")
guess = int(temp)
turns = turns - 1
if guess == answer:
print("It took you", counts,
"turns to guess my number, which was", answer, ".")
break
else:
if counts == 5:
counts = counts + 1
continue
if guess < answer:
print("Turns ", counts+1)
print("Your guess,", guess, ",is too low")
print("Pick a number between 1 and 10.")
print("You have ", turns, " turns left.")
else:
print("Turns ", counts+1)
print("Your guess,", guess, ",is too high")
print("Pick a number between 1 and 10.")
print("You have ", turns, " turns left.")
counts = counts + 1
else:
print("Oops!!No turns left.My number was", answer, ".")
|
|