Python-循环骰子掷游戏时重放
如果用户在被询问是否要再次掷骰子后键入无效响应,我应该如何让下面的循环重播 如果不干扰while循环,我无法让它工作。这是我到目前为止所拥有的Python-循环骰子掷游戏时重放,python,loops,if-statement,while-loop,dice,Python,Loops,If Statement,While Loop,Dice,如果用户在被询问是否要再次掷骰子后键入无效响应,我应该如何让下面的循环重播 如果不干扰while循环,我无法让它工作。这是我到目前为止所拥有的 # Ask the player if they want to play again another_attempt = input("Roll dice again [y|n]?") while another_attempt == 'y': roll_guess = int(input("Please enter your
# Ask the player if they want to play again
another_attempt = input("Roll dice again [y|n]?")
while another_attempt == 'y':
roll_guess = int(input("Please enter your guess for the roll: "))
if roll_guess == dicescore :
print("Well done! You guessed it!")
correct += 1
rounds +=1
if correct >= 4:
elif roll_guess % 2 == 0:
print("No sorry, it's", dicescore, "not", roll_guess)
incorrect += 1
rounds +=1
else:
print("No sorry, it's ", dicescore, " not ", roll_guess, \
". The score is always even.", sep='')
incorrect += 1
rounds +=1
another_attempt = input('Roll dice again [y|n]? ')
if another_attempt == 'n':
print("""Game Summary""")
else:
print("Please enter either 'y' or 'n'.")
我建议您使用两个while循环,并使用函数使代码逻辑更加清晰
def play_round():
# Roll dice
# Compute score
# Display dice
# Get roll guess
def another_attempt():
while True:
answer = input("Roll dice again [y|n]?")
if answer == 'y':
return answer
elif answer == 'n':
return answer
else:
print("Please enter either 'y' or 'n'.")
def play_game():
while another_attempt() == 'y':
play_round()
# Print game summary
while循环当前的条件是什么?你能想到在你想循环的每一种情况下,另一次尝试都是正确的,而在你不想循环的每一种情况下都是错误的吗?提示:为了不重试,您希望用户键入什么内容?