【发布时间】:2014-02-10 05:21:49
【问题描述】:
编辑:谢谢,问题已得到解答!
该程序运行正常,除了它不会循环以允许用户玩新游戏这一事实。即,在输入太多、太少或完美的更改量后,程序会询问“再试一次(y/n)?:”。但是我不知道为什么它不循环......而且当它循环时,它不需要包含关于解释游戏的大段。只是关于“输入加起来为“+str(number)+”美分的硬币,每行一个。”这一行。有什么建议吗?
#Setup
import random
playagain = "y"
#Main Loop
if (playagain == "y"):
number = random.randint(1,99) #Generation of how many cents
total = 0 #Running sum of guessed coins.
print("The purpose of this exercise is to enter a number of coin values")
print("that add up to a displayed target value. \n")
print("Enter coins values as 1-penny, 5-nickel, 10-dime,and 25-quarter.")
print("Hit return after the last entered coin value.\n")
print("Enter coins that add up to "+str(number)+" cents, one per line.\n")
while (True):
if (total == 0):
word = "first"
else:
word = "next"
guess = str(input("Enter "+str(word)+" number: ")) #Records coin value
#Entry Validation
if (guess == ""): #When user is done guessing.
if (total < number):
print("Sorry - you only entered "+str(total)+" cents.\n")
break
elif (total > number):
print("Sorry - total amount exceeds "+str(number)+" cents.\n")
break
else:
print("Correct!")
break
elif (int(guess) == 1) or (int(guess) == 5) or (int(guess) == 10) or (int(guess) == 25):
total = total + int(guess)
else:
print("Invalid entry")
playagain = str(input("Try again (y/n)?: ")) #BRETT: I can't seem to get this to loop properly.
【问题讨论】:
-
在此处仅粘贴代码的相关部分可能会得到更好的答案。
-
如果您认为问题已回答,请接受!
标签: python loops nested-loops