Welcome to Admin Junkies, Guest — join our community!

Register or log in to explore all our content and services for free on Admin Junkies.

Python The "or" Operator and Conditional Statements

Jason

Royal member
Bronze Member
Joined
Nov 3, 2022
Messages
1,446
Website
drummerlesson.com
Credits
31
Conditional statements are very vital for obvious reasons For instance, a program might need input and then it would need to analyze the input based on conditions. For instance, if there was some questionnaire about your health, then it might prescribe some meds based on what you said, but I don't think most people would trust a computer. ;)

Diving In

Let's look at some examples. I don't think this is one of the more difficult topics in Python or programming. It's quite obvious. Do you agree or maybe not? Anyway, we will use the "or" operator.

Code:
your_grade = input("What is your passing grade?")

if your_grade == "A" or your_grade == "a":

    print("You probably tried very hard because your grade is" + " " + your_grade + ".")
 
elif your_grade == "B" or your_grade == "b":

    print("You probably tried hard because your grade is" + " " + your_grade + ".")
 
elif your_grade == "C" or your_grade == "c":

    print("You probably didn't try as hard as you could have because your grade is" + " " +  your_grade + ".")
 
elif your_grade == "D" or your_grade == "d":

    print("You barely passed because your grade is." + " " +  your_grade + ".")
 
else:

    print("Sorry, only passing grades can be analyzed.")

Is all this clear as mud? Notice that equal signs assign things to variables but double equal signs clarify if the stuff on both sides of it are true. For instance, in the first one the double stuff wanted to know if, indeed, your_grade did equal A.

Note: The else statement basically returns the statement it does when you didn't type in A, B, C, or D. It is assumed you would have typed an F, but any letter or letters besides the ones mentioned would have triggered and typing in non-string things, would have just caused an error. Is that last thing true?
 
Last edited:
Definitely need to be a little more careful about the quotes there... can answer the last question when the quotes are fixed ;)
 
No, it really wasn't. Took me a couple of goes at it with a Python interpreter because I'm a bit rusty.

Code:
your_grade = input("What is your passing grade?") # Extra bracket here

if your_grade == 'A': # These all need to be quoted otherwise you get "NameError: name 'A' is not defined"

    print("You probably tried very hard because your grade is " + your_grade + ".") # Was missing quotes and a +
 
elif your_grade == 'B': # elseif isn't valid Python, even if it is in other languages, because it's ambiguous syntax; it's explicitly elif in Python

    print("You probably tried hard because your grade is " + your_grade + ".")
 
elif your_grade == 'C':

    print("You probably didn't try as hard as you could have because your grade is " + your_grade + ".")
 
elif your_grade == 'D':

    print("You barely passed because your grade is " + your_grade + ".")
 
else:

    print("Sorry, only passing grades can be analyzed.")
 
No, it really wasn't. Took me a couple of goes at it with a Python interpreter because I'm a bit rusty.

Code:
your_grade = input("What is your passing grade?") # Extra bracket here

if your_grade == 'A': # These all need to be quoted otherwise you get "NameError: name 'A' is not defined"

    print("You probably tried very hard because your grade is " + your_grade + ".") # Was missing quotes and a +
 
elif your_grade == 'B': # elseif isn't valid Python, even if it is in other languages, because it's ambiguous syntax; it's explicitly elif in Python

    print("You probably tried hard because your grade is " + your_grade + ".")
 
elif your_grade == 'C':

    print("You probably didn't try as hard as you could have because your grade is " + your_grade + ".")
 
elif your_grade == 'D':

    print("You barely passed because your grade is " + your_grade + ".")
 
else:

    print("Sorry, only passing grades can be analyzed.")
Is going thru good now?
 
Mine works on Python 3.8, I tested it by actually running it.

In answer to your original last question, you'll get the 'only passing grades' comment even if you enter 'a' or 'b' or similar because it's only checking for the uppercase letters. But figuring out the 'all the other cases' is a big part of programming logic.
 
Mine works on Python 3.8, I tested it by actually running it.

In answer to your original last question, you'll get the 'only passing grades' comment even if you enter 'a' or 'b' or similar because it's only checking for the uppercase letters. But figuring out the 'all the other cases' is a big part of programming logic.
How to get around that problem so I can edit the post?
 
Quickest way is to force it to uppercase when passing it to your_grade in the first place.

Something like:
Code:
your_grade = input("What is your passing grade?").upper()
 
You should probably install Python and run it yourself - the feedback it gives you will be much faster than asking me to run it for you.
 

Log in or register to unlock full forum benefits!

Log in or register to unlock full forum benefits!

Register

Register on Admin Junkies completely free.

Register now
Log in

If you have an account, please log in

Log in
Activity
So far there's no one here

Users who are viewing this thread

Would You Rather #9

  • Start a forum in a popular but highly competitive niche

    Votes: 5 21.7%
  • Initiate a forum within a limited-known niche with zero competition

    Votes: 18 78.3%
Win this space by entering the Website of The Month Contest

Theme editor

Theme customizations

Graphic Backgrounds

Granite Backgrounds