我正在尝试制作一个需要两个人玩的石头剪刀代码,但是当我尝试使用while
循环时,它会不断打印答案。
代码:
play = input("Do you want to play?")
player1 = input("Rock, paper or scissors?")
player2 = input("Rock, paper or scissors?")
while play == "no":
print("Why are you wasting my time? HUH?")
while play == "yes":
if (player1 == 'rock' and player2 == 'scissors'):
print ("Player 1 wins.")
elif (player1 == 'rock' and player2 == 'rock'):
print ("Tie")
elif (player1 == 'scissors' and player2 == 'paper'):
print ("Player 1 wins.")
elif (player2 == 'scissors' and player2 == 'scissors'):
print ("Tie")
elif (player1 == 'paper' and player2 == 'paper'):
print ("Tie")
elif (player1 == 'paper' and player2 == 'scissors'):
print ("Player 2 wins.")
elif (player1 == 'rock'and player2 == 'paper'):
print ("Player 2 wins.")
elif (player1 == 'paper' and player2 == 'rock'):
print ("Player 2 wins.")
elif (player1 == 'scissors' and player2 == 'rock'):
print ("Player 2 wins.")
else:
print ("This is not a valid object selection.")
答案 0 :(得分:1)
问题是在您输入任一循环后play
永不改变。因此循环永远重复。您还必须将input
移动到循环中。
我只使用一个while循环重新编写代码,因为这更容易做,如果玩家想要停止播放,则使用break
取消。否则,两个玩家都被要求移动,然后打印相应的输出。之后,while循环重复整个过程。
我还在if语句中添加了一个else-case,以防在被问到是否要玩时既不回答是也不回答。然后我只打印一条错误消息,然后再次循环。
while True:
play = input("Do you want to play?")
if play == "no":
print("Why are you wasting my time? HUH?")
break # exits the loop if you don't want to play
elif play == "yes":
player1 = input("Rock, paper or scissors?")
player2 = input("Rock, paper or scissors?")
if (player1 == 'rock' and player2 == 'scissors'):
print ("Player 1 wins.")
elif (player1 == 'rock' and player2 == 'rock'):
print ("Tie")
elif (player1 == 'scissors' and player2 == 'paper'):
print ("Player 1 wins.")
elif (player2 == 'scissors' and player2 == 'scissors'):
print ("Tie")
elif (player1 == 'paper' and player2 == 'paper'):
print ("Tie")
elif (player1 == 'paper' and player2 == 'scissors'):
print ("Player 2 wins.")
elif (player1 == 'rock'and player2 == 'paper'):
print ("Player 2 wins.")
elif (player1 == 'paper' and player2 == 'rock'):
print ("Player 2 wins.")
elif (player1 == 'scissors' and player2 == 'rock'):
print ("Player 2 wins.")
else:
print ("This is not a valid object selection.")
else:
print("What?")
答案 1 :(得分:1)
while
将重复循环内的任何内容,直到您给出中断条件。在您的情况下,您可能希望用while
条件替换两个if
循环,因此它不会对您的命令行发送垃圾邮件。
如果你希望它之后自动要求另一个游戏,你可以在游戏中制作一个方法,并提示另一个用户输入,询问另一个游戏是/否。
def game():
if (player1 == 'rock' and player2 == 'scissors'):
print ("Player 1 wins.")
elif (player1 == 'rock' and player2 == 'rock'):
print ("Tie")
elif (player1 == 'scissors' and player2 == 'paper'):
print ("Player 1 wins.")
elif (player2 == 'scissors' and player2 == 'scissors'):
print ("Tie")
elif (player1 == 'paper' and player2 == 'paper'):
print ("Tie")
elif (player1 == 'paper' and player2 == 'scissors'):
print ("Player 2 wins.")
elif (player1 == 'rock'and player2 == 'paper'):
print ("Player 2 wins.")
elif (player1 == 'paper' and player2 == 'rock'):
print ("Player 2 wins.")
elif (player1 == 'scissors' and player2 == 'rock'):
print ("Player 2 wins.")
else:
print ("This is not a valid object selection.")
play = input("Do you want to play?")
player1 = input("Rock, paper or scissors?")
player2 = input("Rock, paper or scissors?")
if play == "no":
print("Why are you wasting my time? HUH?")
elif play == "yes":
game()
another = input("Want to play again?")
if another == "yes":
game()
else:
print("Okay bye then.")