T.Cho
T.Cho

Reputation: 27

Python if statement not working as it should

Just to clarify, I have pasted my whole program as the problem can only be identified when it is with all the code in my program. My problem is, I have in-putted if else and elif statements in my program. When the user inputs "no" the program ends, and this runs fine and if the user inputs "yes" the program advances like it is supposed to do. The problem is when the user will input something invalid, something other than "yes" or "no" it should take them back to retyping their option but instead it carries on as if the user has inputted "yes".

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

Upvotes: 0

Views: 4362

Answers (3)

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23213

You may use continue keyword.

The continue statement, also borrowed from C, continues with the next iteration of the loop:

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()

Upvotes: 2

hegez
hegez

Reputation: 1028

You need to put one while loop more for that, returning the user in syntactically wrong answer and breaking loop in right answer. The question part should look like this:

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

Upvotes: 0

Andrea Dusza
Andrea Dusza

Reputation: 2110

You only wrote a print statement in the else clause: print("Invalid entry, please type again"), but that does not make the program repeat the previous instructions. A possible solution:

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")

Upvotes: 1

Related Questions