【问题标题】:Why does the if and elif function not work properly in my code [duplicate]为什么 if 和 elif 函数在我的代码中无法正常工作 [重复]
【发布时间】:2019-08-14 16:19:30
【问题描述】:

我是代码的初学者。我已经为我的家庭作业编写了这段代码(这是一个精简版),但是每当我按下播放并开始这个过程时,无论我输入多少边,它总是要求一些平行边,然后猜测和等腰三角形.为什么会这样?

print("Shape Guesser Game \n-----------------")
print("Welcome to the Shape Guesser Game! \nI believe that I can guess the shape you think of.")
print("There is only 1 rule: \nYou can only think of a polygon with a maximum of 6 sides.")
response_1 = input("\nDo you want to play? \nPick Yes or No")
if response_1 == "Yes" or "yes":
    print("Great! Let's get started! \n------------------")
    response_2 = input("Have you thought of your shape? Yes/No")
    if response_2 == "Yes" or "yes":
        response_3 = input("How many sides does it have?")
        if response_3 == "3" or "three":
            response_4 = input("How many equal sides does it have?")
            if response_4 == "2" or "two":
                response_5 = input("Aha! It is an isosceles triangle! Am I right? Yes or No?")
                if response_5 == "yes" or "Yes":
                    print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")

            elif response_4 == "3" or "three":
                response_6 = input("Aha! It is an equilateral triangle! Am I right? Yes or No?")
                print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")

            else:
                print("Not valid:Error \n-------------")

        elif response_3 == "4" or "four":
            response_7 = input("How many parallel sides does it have?")
            if response_7 == "two" or "2":
                response_8 = input("Aha! It is a trapezium! Am I right? Yes or No?")
                if response_8 == "yes" or "Yes":
                    print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
            if response_7 == "four" or "4":
                response_9 = input("Aha! It is a parallelogram! Am I right? Yes or No?")
                if response_9 == "yes" or "Yes":
                    print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")

    elif response_2 == "No" or "no":
        print("Okay no worries, I will give you more time!")

elif response_1 == "No" or "no":
    print("Sad to see you go! Back to Home.")

【问题讨论】:

  • 除了通过将if response_1 == "Yes" or "yes"更改为if response_1 == "Yes" or response_1 == "yes"来修复之外,您还可以将if作为if response_1 in ("Yes", "yes")进行检查。当您需要与多个值进行比较时,这可能会很好。

标签: python


【解决方案1】:

实际上,每个 if 语句都有相同的问题。当你写类似的东西时

if response_1 == "Yes" or "yes"

python 将其解释为

if (response_1 == "Yes") or ("yes")

即:如果变量response_1等于“Yes”,或者如果字符串“yes”为真,则if语句运行。 “是”是真实的,所以 if 语句总是运行。 (请参阅here,了解更多关于真实性意味着什么的信息)。

您可能想要检查变量 response_1 是否等于“是”或是否等于“是”,写为:

if response_1 == "Yes" or response_1 == "yes"`

当然,您还需要对其余的 if 语句应用同样的更正。

【讨论】:

  • 啊啊啊我现在明白了!!非常感谢你:))))
【解决方案2】:

使用这个:

print("Shape Guesser Game \n-----------------")
print("Welcome to the Shape Guesser Game! \nI believe that I can guess the shape you think of.")
print("There is only 1 rule: \nYou can only think of a polygon with a maximum of 6 sides.")
response_1 = input("\nDo you want to play? \nPick Yes or No")
if response_1.lower() == "yes":
    print("Great! Let's get started! \n------------------")
    response_2 = input("Have you thought of your shape? Yes/No")
    if response_2.lower() == "yes":
        response_3 = input("How many sides does it have?")
        if response_3 == "3" or response_3 == "three":
            response_4 = input("How many equal sides does it have?")
            if response_4 == "2" or response_4 == "two":
                response_5 = input("Aha! It is an isosceles triangle! Am I right? Yes or No?")
                if response_5.lower() == "yes":
                    print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")

            elif response_4 == "3" or response_4 == "three":
                response_6 = input("Aha! It is an equilateral triangle! Am I right? Yes or No?")
                print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")

            else:
                print("Not valid:Error \n-------------")

        elif response_3 == "4" or response_3 == "four":
            response_7 = input("How many parallel sides does it have?")
            if response_7 == "two" or response_7 == "2":
                response_8 = input("Aha! It is a trapezium! Am I right? Yes or No?")
                if response_8.lower() == "yes":
                    print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
            if response_7 == "four" or response_7 == "4":
                response_9 = input("Aha! It is a parallelogram! Am I right? Yes or No?")
                if response_9.lower() == "yes":
                    print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")

    elif response_2.lower() == "no":
        print("Okay no worries, I will give you more time!")

elif response_1.lower() == "no":
    print("Sad to see you go! Back to Home.")

or 不像if a == 'b' or 'c': 那样工作,你必须让它if a == 'b' or a == 'c':

还有像if response_1 == "Yes" or "yes" 这样的东西可以变成if response_1.lower() == "yes",所以YeSyEs 会被认为是'yes'

【讨论】:

  • 我解决了!非常感谢:)))
  • @JuihiCharadva 如果有效,请采纳
猜你喜欢
  • 2022-07-08
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 2017-02-03
相关资源
最近更新 更多