该程序正常运行,除了它不循环以允许用户玩新游戏。即,在输入太多,太少或完美的变化量之后,程序会问“再试一次(是/否)?:”。但我无法找出它为什么不循环...当它循环时,它不需要包含关于解释游戏的大段落。只是关于“输入加起来的硬币+ + str(数字)+”美分,每行一个。“有什么提示吗?
#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.
答案 0 :(得分:1)
通过使用break,你完全离开了while循环,从不检查playagain
条件。如果你想看看用户是否想要再次播放,请将'playagain'检查放入另一个循环中。
#Setup
import random
playagain = "y"
#Main Loop
while (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.
答案 1 :(得分:0)
您将playagain
设置为y / n,但如果playagain等于'y',则代码不会回到开头。尝试将if playagain == "y"
转换为while playagain == "y"
。这样,如果playagain仍然设置为“y”,它会第一次进入并继续回到开头。
另外,缩进最后一行(playagain = str(....)
),使其成为while playagain == "y"
循环的一部分。如果不是,则代码将陷入无限循环,因为在while
循环内没有更改playagain。
答案 2 :(得分:0)
将最后一行缩进到while True
行。并将if (playagain == "y"):
更改为
while (playagain == "y"):
答案 3 :(得分:0)
你的&#34;主循环&#34;不是循环,它只是一个if语句。另外最好使用raw_input,因为输入将评估您的输入。尝试一下这一点:
playagain = 'y'
#Main loop
while playagain == 'y':
print "do gamelogic here..."
playagain = raw_input("Try again (y/n)?: ")
在你的gamelogic中,你可以使用布尔值检查是否需要打印游戏说明:
show_explanation = True
while playagain == 'y':
if show_explanation:
print "how to play is only shown once..."
show_explanation = False
print "Always do this part of the code"
...
playagain = raw_input("Try again (y/n)?: ")