Python Forum
If statement not working correctly? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: If statement not working correctly? (/thread-38713.html)



If statement not working correctly? - MrKnd94 - Nov-15-2022

Hello.

I'm sorry to bother you all, but I'm having trouble with the While loop, and the Quit function. I've tried it before, but I didn't get it to work, unfortunately. But I want it to work now. I'm ready to learn!

So, here's the code (with comments), and I don't know what to do with it:

quit = False # New boolean value to check if the user wants to quit or not.
    
while quit == False: # While Loop to check if quit is False, then carry on
    input_edq = input('Pres the letter "E", then press enter to encrypt text, press the letter "D", then press enter to decrypt text and press the letter "Q", then press enter to exit: ') # Input the text E, D or Q. If the user wants to not enter the correct letter, see below...

    if input_edq == "q" or "Q":
        print("You're exiting the program. Farewell!")
        quit = True
        if quit == True:
            exit()
                
            if input_edq == "e" or "E":
                message = str(input('Enter a message '))
                shift = int(input('Enter a shift number  '))

                if input_edq == "d" or "D":
                    message = str(input('Enter a message '))
                    shift = int(input('Enter a shift number  '))
                else:
                    input_edq = input('Invalid response! Pres the letter "E", then press enter to encrypt text, press the letter "D", then press enter to decrypt text and press the letter "Q", then press enter to exit: ') # If it's anything but the valid response, then print this out and input new command.
So, you see I only want it to quit when the user inputs "q" or "Q", however that's not the case. It quits automatically, whenever I input anything.

Why does Python do this? Is there any workarounds? I'm in week 8 of learning it, and I just don't get why it does this.

If there are any workarounds, please can I see them? Because this is important to me.

I hope you all understand.

Thank you!


RE: ? - rob101 - Nov-15-2022

It's a common misconception, because you're reading it like English.

Try:

 if input_edq == "q" or input_edq == "Q":
        print("You're exiting the program. Farewell!")
        exit()
The same goes for lines 12 and 16


RE: If statement not working correctly? - deanhystad - Nov-16-2022

This does not do what you think it does:
if input_edq == "q" or "Q":
In Python "a or b" returns a if a is truthy or it returns b.
Output:
a or b _______|______ _|_ | | | | input_edq == "q" or "Q"
In the example above a = input_edq == "q" and b = "Q".

If the user enters "D", python first evaluates input_edq == "q". This is False, so Python returns "Q". You can see this by running the program below:
input_edq = "q"
print(input_edq == "q" or "Q")
input_edq = "D"
print(input_edq == "q" or "Q")
Output:
True Q
The program prints True when input+edq == "q", otherwise it prints "Q". This means that the result
Output:
input_edq == "q" or "Q"
will always be True or "Q". Both of these happen to be "truthy", so the code inside the if statement is executed.
As rob101 mentions, the correct way to write this particular test is to make both "a" and "b" perform a comparison.
input_edq = "q"
print(input_edq == "q" or input_edq == "Q")
input_edq = "D"
print(input_edq == "q" or input_edq == "Q")
Output:
True False
I don't like compound if statements because they are easy to get wrong (as you well know!). I might write it like this.
if input_edq in ("q", "Q"):
Or I might convert input_edq to upper case so I don't have to test if it matches lower case letters.

You must really like typing. You user is not going to enjoy reading all that typing. Shorten things up a bit.
input_edq= input("What do you want to do? (E)ncrypt (D)ecrypt (Q)uit: ").upper()
Take a look at this and think about it a bit:
if input_edq == "E":
    message = str(input('Enter a message '))  # input() returns a str.  Don't have to convert to str.
    shift = int(input('Enter a shift number  '))
 
if input_edq == "D":
    message = str(input('Enter a message '))
    shift = int(input('Enter a shift number  '))
Do you really need to ask for the message and the shift in to different places? Can't you do this just once and then call encrypt or decrypt based on the the value of input_edq?\

In case you didn't notice from the code above, your indentation is wrong. The way your program is written the only thing you can do is quit. All other choices are ignored because of how you are indenting your code.

You should start out simple and work your way up. Start with "loop until user inputs Q". When you have that working add in "Print 'encrypt' when user enters E and 'decrypt when user enters D". Then add in the code so the user can enter upper or lower case. Keep adding functionality until you have all the functionality.