【发布时间】:2015-02-08 02:24:59
【问题描述】:
我正在制作一个石头剪刀布游戏。一切正常,除了我不明白为什么第二个 while 循环在这段代码中不起作用。我想制作这个程序,如果用户没有输入“R”或“P”或“S”,那么用户将被告知这是一个无效条目,它会提示用户再次输入他们的答案。它适用于 player1,但不适用于 player2。对于 player2,如果您没有输入“R”或“P”或“S”,那么它会提示您再次输入一个新值,但只输入一次,无论您输入什么。感谢所有帮助!
if playGame == "Y":
print("Ok, here we go.")
player1 = input("Player1, what is your choice, R, P, or S? ")
player1 = player1.upper()
while player1 != 'R' and player1 != 'P' and player1 != 'S':
player1 = input("Invalid answer. Please answer R, P, or S: ")
player1 = player1.upper()
player2 = input("Player2, what is your choice, R, P, or S? ")
player2 = player2.upper()
while player2 != 'R' and player2 != 'P' and player2 != 'S':
player2 = input("Invalid answer. Please answer R, P, or S: ")
player2 = player1.upper()
【问题讨论】:
-
你的代码的最后一行应该如下: player2 = player2.upper
标签: python loops while-loop