首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重复"if“语句,不考虑"elif”

重复"if“语句,不考虑"elif”
EN

Stack Overflow用户
提问于 2021-09-14 02:49:12
回答 2查看 49关注 0票数 0

这是我做的一个简单的游戏,我想添加一个选项,这样就可以再次玩游戏,而不必一遍又一遍地运行程序。然而,每当我尝试这样做时,它似乎没有考虑或到达我代码底部的"elif“语句。我怎么才能让它工作?

代码语言:javascript
复制
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 -----------------------------------------------------
EN

回答 2

Stack Overflow用户

发布于 2021-09-14 02:58:00

代码语言:javascript
复制
   again = input("\nDo you want to play again? (yes/no): \n").lower

这应该是一个打字错误,下边应该是下边()

代码语言:javascript
复制
   if again == "yes" or "ye" or "y"

这个条件可能不合适,因为您可能需要知道什么是运算符优先级https://docs.python.org/3/reference/expressions.html#operator-precedence

我建议你可以使用

代码语言:javascript
复制
  if again in ["yes","ye","y"]:
票数 0
EN

Stack Overflow用户

发布于 2021-09-14 02:59:48

代码语言:javascript
复制
if again == "yes" or "ye" or "y":

应该是这样的

代码语言:javascript
复制
if again == "yes" or again == "ye" or again =="y":

否则,它的计算结果如下所示

代码语言:javascript
复制
if "ye":
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69171239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档