Home > other >  elif statement unreached in Python script
elif statement unreached in Python script

Time:12-29

In the following code, the if statement is reached and runs fine. The elif statement however seems to have no effect. When the script is run, when the conditions for the elif statement are met, nothing happens and when I press return button twice the script just keeps going skipping the elif statement altogether.

My code:

            print("If you want to name this window press 1, if you want to describe it press 2")
            if input() == "1":
                print("Please enter this window's title:")
                current_window_title = input()
                print("Do you also want to describe this window? P.S: You can do this later.)")
                if input().lower() == "yes":
                    print("Please enter this window's description:")
                    current_window_description = input()
                else:
                    current_window_description = "None"
            elif input() == "2":
                print("Please enter this window's description:")
                current_window_description = input()
                print("Do you also want to give this window a title? P.S: You can do this later.")
                if input().lower() == "yes":
                    print("Please enter this window's title:")
                    current_window_title = input()

CodePudding user response:

There are too many undesired input() statements in the code, which are making it work abruptly.

Also, as python's input() function also has the capability of printing, you can combine print() and input() into one statement.

All modifications as follows:
Input = input("If you want to name this window press 1, if you want to describe it press 2")

if Input == "1":
    current_window_title = input("Please enter this window's title:")

    if input("Do you also want to describe this window? P.S: You can do this later.)").lower() == "yes":
        current_window_description = input("Please enter this window's description:")
    else:
        current_window_description = "None"
        

elif Input == "2":
    current_window_description = input("Please enter this window's description:")
    if input("Do you also want to give this window a title? P.S: You can do this later.").lower() == "yes":
        current_window_title = input("Please enter this window's title:")

CodePudding user response:

You must use input variable before if elif else blocks.

answer=input("If you want to name this window press 1, if you want to describe it press 2:")
            if answer == "1":
               Code section 1
           elif answer=="2":
               Code section 2
           else : 
              print ("wrong command")

CodePudding user response:

Your code would ask for input twice if you wan to reach 2. The first time, it would ask for input, it is in the if statement, you would enter 2 and it wouldn't match. The second time it would ask for an input, you are in the elif statement and it would work.

It is better to get the result of the input then evaluate it.

print("If you want to name this window press 1, if you want to describe it press 2")
result = input()
if result == "1":
    print("Please enter this window's title:")
    current_window_title = input()
    print("Do you also want to describe this window? P.S: You can do this later.)")
    if input().lower() == "yes":
        print("Please enter this window's description:")
        current_window_description = input()
    else:
        current_window_description = "None"
elif result == "2":
    print("Please enter this window's description:")
    current_window_description = input()
    print("Do you also want to give this window a title? P.S: You can do this later.")
    if input().lower() == "yes":
        print("Please enter this window's title:")
        current_window_title = input()

CodePudding user response:

You need to move the initial input() of user input outside of the if and elif statements, so that they are comparing to the same value. Currently the elif will never capture that input of "2", unless you enter a second input ("2" again). Try something like this.

print("If you want to name this window press 1, if you want to describe it press 2")
user_input = input()
if user_input == "1":
    print("Please enter this window's title:")
    current_window_title = input()
    print("Do you also want to describe this window? P.S: You can do this later.)")
    if input().lower() == "yes":
        print("Please enter this window's description:")
        current_window_description = input()
    else:
        current_window_description = "None"
elif user_input == "2":
    print("Please enter this window's description:")
    current_window_description = input()
    print("Do you also want to give this window a title? P.S: You can do this later.")
    if input().lower() == "yes":
        print("Please enter this window's title:")
        current_window_title = input()

CodePudding user response:

Because you got an input from the user and in the same range if and this amount of input is not available in elif you can do these two things and it will work properly:

print("If you want to name this window press 1, if you want to describe it press 2")
inp = input()
if inp == "1":
    print("Please enter this window's title:")
    current_window_title = input()
    print("Do you also want to describe this window? P.S: You can do this later.)")
    if input().lower() == "yes":
        print("Please enter this window's description:")
        current_window_description = input()
    else:
        current_window_description = "None"
elif inp == "2":
    print("Please enter this window's description:")
    current_window_description = input()
    print("Do you also want to give this window a title? P.S: You can do this later.")
    if input().lower() == "yes":
        print("Please enter this window's title:")
        current_window_title = input()

or :

print("If you want to name this window press 1, if you want to describe it press 2")
if input() == "1":
    print("Please enter this window's title:")
    current_window_title = input()
    print("Do you also want to describe this window? P.S: You can do this later.)")
    if input().lower() == "yes":
        print("Please enter this window's description:")
        current_window_description = input()
    else:
        current_window_description = "None"
if input() == "2":
    print("Please enter this window's description:")
    current_window_description = input()
    print("Do you also want to give this window a title? P.S: You can do this later.")
    if input().lower() == "yes":
        print("Please enter this window's title:")
        current_window_title = input()
  • Related