我想根据用户输入重置代码,如果是,则重置代码,如果不停止则重置代码。我知道这是一个非常糟糕的代码,很抱歉我还在学习:)
import random
tool = input("Choose your weapon (R)ock (P)aper (S)cissors ")
computer = random.choice(["Rock", "Paper", "Scissors"])
if tool == "R" and computer == "Paper":
print("You lost aganist ",computer,"!")
elif tool == "R" and computer == "Scissors":
print("You won aganist! ",computer,"!")
elif tool == "R" and computer == "Rock":
print("That's a tie!")
elif tool == "P" and computer == "Rock":
print("You won aganist ",computer,"!")
elif tool == "P" and computer == "Scissors":
print("You lost aganist ",computer,"!")
elif tool == "P" and computer == "Paper":
print("That's a tie!")
elif tool == "S" and computer == "Rock":
print("You lost aganist ",computer,"!")
elif tool == "S" and computer == "Paper":
print("You won aganist ",computer,"!")
elif tool == "S" and computer == "Scissors":
print("That's a tie!")
将其放在while循环中,并要求重复此操作,如果他拒绝,则将bool设置为no。顺便说一句,这忽略了错误的输入,您应该检查他的输入是否合法,就像我在这里所做的那样
import random
playing = True
while playing:
tool = input("Choose your weapon (R)ock (P)aper (S)cissors ")
computer = random.choice(["Rock", "Paper", "Scissors"])
if tool == "R" and computer == "Paper":
print("You lost aganist ",computer,"!")
#...
elif tool == "S" and computer == "Scissors":
print("That's a tie!")
else:
print("Wrong input! Sorry!")
play_again = input("Do you want to play again? y/n")
if (play_again == "n"):
playing = False
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句