【问题标题】:Python if statement not working as it shouldPython if 语句无法正常工作
【发布时间】:2016-04-14 21:42:29
【问题描述】:

为了澄清,我已经粘贴了我的整个程序,因为只有在我的程序中的所有代码出现问题时才能识别问题。我的问题是,我在我的程序中输入了 if else 和 elif 语句。当用户输入“否”时,程序结束,并且运行良好,如果用户输入“是”,则程序按预期进行。问题是当用户输入一些无效的内容时,除了“是”或“否”之外,它应该让他们重新输入他们的选项,但它会继续进行,就好像用户输入了“是”一样。

while True:
    print (" Position Finder")
    choice = (input("Do you want to run the Position Finder program? Please enter yes or no!"))
    # Lets the user input an answer to question I am asking it
    if choice.lower() == "no":
        # If the user input is "no" 
        print("The program will not run")
        # This statement will be printed out and the program will not run
        quit()
    elif choice.lower() == "yes":
        # If the user input is "yes"
        print("The program will now run")
    # This statement will be printed and the program will run
    else:
        # If an invalid statement is inputted
        print("Invalid entry, please type again")

    # This statement will be printed and the user will be given the option to input again


    sentList = "ask not what your country can do for you ask what you can do for your country"
    # Creates a list
    sentList2 = sentList.split()
    # This will split the list and give each word a position
    print (sentList)
    # This will print out the sentence in the program 

    word = (input("Enter Word: "))
    # Lets the user input a word from the sentence
    wordFound = False
    # This boolean statement, "wordFound" will be set to true when the word has been found in the list
    for (num, x) in enumerate(list(sentList2)):
        # For every word in the users sentence
        while word.lower() == x:
            # While the users word is equal to the position in the sentence
            print  (ordinalSuffix())
            # Print the ordinal suffix function
            wordFound = True
            # This boolean statement has been set to true as the word has been found in the list
            break
    # This will break the while loop from the boolean variable called "wordFound"


    if wordFound == False:
        # If the word has not been found in the list and the "wordFound" boolean variable has not been set to true
        print ("Please enter a word from the sentence")
        # This statement will print,meaning the user has to enter a word from the list

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    您可以使用continue 关键字。

    continue 语句,也是从 C 中借来的,继续下一个 循环的迭代:

    while True:
        print (" Position Finder")
        choice = (input("Do you want to run the Position Finder program? Please enter yes or no!"))
        if choice.lower() == "no":
            print("The program will not run")
            quit()
        elif choice.lower() == "yes":
            print("The program will now run")
        else:
            print("Invalid entry, please type again")
            continue  # move to next iteration
        rest_of_your_code()
    

    【讨论】:

    • 我已经尝试使用您的解决方案,这修复了错误,但现在每次我输入“是”时,它都会循环回到相同的问题“您要运行位置查找器程序吗?”
    • 在 Python 中,缩进是语法的一部分。我猜你的 continue 在 else 块之外(无论选择什么都会执行)。
    • “继续”就像您提供的解决方案一样,位于“打印”语句下方
    • 混合制表符和空格?
    • 仍然出现同样的问题。
    【解决方案2】:

    您只在 else 子句中写了一条打印语句:print("Invalid entry, please type again"),但这不会使程序重复前面的指令。一个可能的解决方案:

    choice = ""
    while choice.lower() != "yes":
        choice = (input("Do you want to run the Position Finder program? Please enter yes or no!"))
        if choice.lower() == "no": 
                print("The program will not run")
                quit()
        elif choice.lower() == "yes":
                print("The program will now run")
        else:
                print("Invalid entry, please type again")
    

    【讨论】:

    • 我已经尝试过你写的,但我得到一个'unindent 不匹配任何外部缩进级别
    • 注意缩进。如果有空格和制表符混合,请将所有制表符替换为空格。
    • 如果你看到我在 Rogalski 的回答中提供的 cmets,同样的情况会发生,当输入“是”时,它会不断循环问题(“你想运行定位器程序吗? ?")
    【解决方案3】:

    您需要为此添加一个while 循环,以语法错误的答案返回用户并在正确答案中中断循环。问题部分应如下所示:

    while True:   # Main loop
        while True:   # Question loop
            choice = (input("Do you want to run the Position Finder program? Please enter yes or no!"))
    
            if choice.lower() == "no": 
                print("The program will not run")
                run = False
                break
    
            elif choice.lower() == "yes":
                print("The program will now run")
                run = True
                break
    
            else:
                print("Invalid entry, please type again")
                # In case of invalid entry Question loop will run again
    
        # Outside Question loop
        # Let's check if we should run the program
        if not run:
            break
            # This will be executed if *run* is False, it will break the Main loop
    
        # Your program
    

    【讨论】:

    • 抱歉回复晚了,但是当我使用你的版本时,它说“运行”没有定义
    • 抱歉回复晚了,我现在修好了。
    猜你喜欢
    • 2019-07-23
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多