这是我做的一个简单的游戏,我想添加一个选项,这样就可以再次玩游戏,而不必一遍又一遍地运行程序。然而,每当我尝试这样做时,它似乎没有考虑或到达我代码底部的"elif“语句。我怎么才能让它工作?
import random
from time import sleep as wait
# Definition of variables.
choices = [
"rock",
"paper",
"scissors"
]
ai = random.choice(choices)
player = input("Enter rock, paper, or scissors: ").lower()
while True:
for i in range(2147483647):
# Makes sure the player types in something from the choices.
if player not in choices:
print("\nInvalid! Enter a valid choice. (Check your spelling!)")
wait(0.5)
player = input("\nEnter rock, paper, or scissors: ").lower()
else:
print(f"\nYou picked {player}.")
break
wait(1)
print(f"\nai picked {ai}.\n")
wait(1)
# Every possible solution for the choices.
if player == "rock" in choices and ai == "paper" in choices:
print("The ai covered you in paper. (Lose)")
elif player == "rock" in choices and ai == "scissors" in choices:
print("You beat the ai's scissors to a plump. (Win)")
elif player == "rock" in choices and ai == "rock" in choices:
print("Y'all are beating each other with a rock, and no one wins. (Tie)")
elif player == "paper" in choices and ai == "rock" in choices:
print("You cover ai's rock in paper. (Win)")
elif player == "paper" in choices and ai == "paper" in choices:
print("Both of you try to cover each other in paper, endlessly. (Tie)")
elif player == "paper" in choices and ai == "scissors" in choices:
print("ai cuts you to pieces! (Lose)")
elif player == "scissors" in choices and ai == "rock" in choices:
print("ai beats your scissors to a plump with a rock. (Lose)")
elif player == "scissors" in choices and ai == "paper" in choices:
print("You cut ai to pieces! (Win)")
elif player == "scissors" in choices and ai == "scissors" in choices:
print("Y'all try to cut each other's metal somehow, endlessly. (Tie)")
else:
# This is in case the player found a bug.
print("How did we get here?")
wait(1)
again = input("\nDo you want to play again? (yes/no): \n").lower
# Start of problem ---------------------------------------------------
if again == "yes" or "ye" or "y":
ai = random.choice(choices)
player = input("Enter rock, paper, or scissors: ").lower()
continue
elif again == "no" or "n":
break
# End of problem -----------------------------------------------------发布于 2021-09-14 02:58:00
again = input("\nDo you want to play again? (yes/no): \n").lower这应该是一个打字错误,下边应该是下边()
if again == "yes" or "ye" or "y"这个条件可能不合适,因为您可能需要知道什么是运算符优先级https://docs.python.org/3/reference/expressions.html#operator-precedence
我建议你可以使用
if again in ["yes","ye","y"]:发布于 2021-09-14 02:59:48
if again == "yes" or "ye" or "y":应该是这样的
if again == "yes" or again == "ye" or again =="y":否则,它的计算结果如下所示
if "ye":https://stackoverflow.com/questions/69171239
复制相似问题