【发布时间】:2015-10-15 18:40:30
【问题描述】:
对于一个学校项目,我一直在制作一个游戏,该游戏允许用户猜测足球比赛的最终结果并能够投注预测。如果他们的余额小于或等于零,我一直试图打破 while 循环,但一直无法这样做。整个 Python 代码比下面显示的要广泛得多,但这是唯一的问题。
tries = int(input("How many attempts would you like : "))
while balance > tries:
while guesses < 2:
print ("\nYour current balance is : £",balance)
gameresultguess = input("\nWho will win - Home, Away, or Draw : ")
bettingchoice = input("Would you like to bet this round - Yes or No : ")
while True:
if str.lower(bettingchoice) == "yes":
bet = int(input("How much would you like to bet : "))
if bet > balance: break
print ("You have insuficient funds to bet this much, please try again")
guesses +=1
hometrue = random.randint(1,7)
awaytrue = random.randint(1,5)
if hometrue > awaytrue:
gameresulttrue = "home"
if awaytrue > hometrue:
gameresulttrue = "away"
if hometrue == awaytrue:
gameresulttrue = "draw"
if str.lower(gameresultguess) == gameresulttrue:
print ("Correct! Nice guess! The final score was ",hometrue," : ",awaytrue)
if str.lower(bettingchoice) == "yes":
balance = (balance + bet)
print ("Well played! Your bet has been doubled and added to your balance")
if str.lower(bettingchoice) == "no":
print ("Unlucky... Should have placed a bet")
else:
print ("Unlucky! The final score was : ",hometrue," : ",awaytrue)
if str.lower(bettingchoice) == "yes":
balance = (balance - bet)
print ("Oops... Better luck next time")
print ("Ouch... You went bankrupt. Try coming back when you have more money")
【问题讨论】:
-
我不知道你在哪里试过这个。您的代码中还有其他 break 语句,因此应该直截了当地说 if balanace
标签: python python-3.x if-statement while-loop