mardi 28 mars 2017

Why does my python script stop at if-else conditions?

I had written a python script with if-elseif conditions which when satisfied, would perform some task. I am taking an input from the user (1st input), and if that matches the if condition, it would execute the piece of code within that conditional block. That conditional block again contains if-else conditions and would ask the user for input again (2nd input). When i look at the code, it seems fine but when i try to execute it, the script runs successfully one time, and next time it just stops after receiving the user's input (1st input).

Below is my code:

choice1 = int(input("Enter Number 1"))

if choice1==1:
    choice2 = int(input("Enter Number 2"))
    if choice2==1:
         print "You have entered 1 - 1"
    elif choice2==2:
         print "You have entered 1 - 2"
    else:
         pass

elif choice1==2:
    choice2 = int(input("Enter Number 2"))
    if choice2==1:
         print "You have entered 2 - 1"
    elif choice2==2:
         print "You have entered 2 - 2"
    else:
         pass

elif choice1==3:
    choice2 = int(input("Enter Number 2"))
    if choice2==1:
         print "You have entered 3 - 1"
    elif choice2==2:
         print "You have entered 3 - 2"
    else:
         pass

elif choice1==4:
    choice2 = int(input("Enter Number 2"))
    if choice2==1:
         print "You have entered 4 - 1"
    elif choice2==2:
         print "You have entered 4 - 2"
    else:
         pass

else:
     pass

So when i run the script, sometimes it asks me for 'Number 1 ' followed by 'Number 2' and the script runs successfully, and sometimes the script gets stuck after asking me for 'Number 1'. I have to manually disrupt the execution and run the script again. Does anyone know what i am doing wrong? I am using 'Anaconda Prompt' for running the script.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire