我目前正在开发一个头部或尾巴的游戏。所以它会循环,直到健康状态达到0,但当它达到0时,它会中断try except循环。但当它到达最后时,它只会显示“很遗憾,那不是右边!”然后就停下来..。不知道为什么任何帮助都是非常感谢的。
代码:
import random
global health
try:
global health
health = 3
while health != 0:
print ("Pick a side of the coin. Heads or Tails?")
print ("Your health is:", health)
input_coin = input()
input_coin = input_coin.lower()
coin = random.choice(["heads", "tails"])
if input_coin == coin:
print ("You picked the right side!")
health = health + 1
continue
elif health == 0:
break
else:
print ("Sadly, that is not the right side!")
health = health - 1
continue
except:
print("Youve run out of lives!")
发布于 2018-06-21 07:01:27
要在运行状况达到零时显示消息,只需在while
循环中添加一个else
子句。
try except
循环用于捕获错误,您的代码不会抛出任何错误。
这会做你想做的事。
import random
health = 3
while health != 0:
print ("Pick a side of the coin. Heads or Tails?")
print ("Your health is:", health)
input_coin = input()
input_coin = input_coin.lower()
coin = random.choice(["heads", "tails"])
if input_coin == coin:
print ("You picked the right side!")
health = health + 1
continue
elif health == 0:
break
else:
print ("Sadly, that is not the right side!")
health = health - 1
continue
else:
print("You’ve run out of lives!")
https://stackoverflow.com/questions/50957627
复制相似问题