Python Forum
If statement not working correctly?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement not working correctly?
#1
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!
Reply
#2
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
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question If, elif, and else statement not working PickleScripts 3 902 Mar-30-2023, 02:53 PM
Last Post: PickleScripts
  If Statement not working...Why? Milfredo 2 2,212 Oct-17-2020, 03:23 AM
Last Post: Milfredo
  random.uniform is not working correctly dron4ik86 9 4,134 Feb-06-2020, 03:04 PM
Last Post: dron4ik86
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,220 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Invoking function in if else statement, not working! ibaad1406 13 5,640 May-30-2019, 09:05 PM
Last Post: ibaad1406
  if /else statement not executing correctly bluethundr 3 2,794 Apr-15-2019, 10:20 PM
Last Post: bluethundr
  If statement not working oldcity 4 3,108 Oct-14-2018, 10:45 AM
Last Post: oldcity
  How do you know when a library is working correctly? grjmmr 1 2,422 Jul-24-2018, 03:08 PM
Last Post: buran
  if statement not working trent101010 8 4,967 Mar-14-2018, 03:19 PM
Last Post: wavic
  why is this try.....except statement not working? HenryJ 3 8,771 Feb-06-2018, 06:15 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020