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 "and" Operator and Conditional Statements

Jason

Royal member
Bronze Member
Joined
Nov 3, 2022
Messages
1,446
Website
drummerlesson.com
Credits
31
Let's look at the "and" operator in Python. It can expand things so we can make more complex things. Well, in this example, a user is going to be asked for input on what his/her age is and the program will determine what school he/she is probably in.

Code:
your_age = int(input("What is your under 18 child's age?"))

while your_age <= 11 and your_age >= 5:

        print("Since the age submitted is" + " " + str(your_age) + " " + "you are probably in elementary school.")
 
if your_age < 5:

        print("Since the age submitted is" + " " +  str(your_age) +  " " + "you are probably in preschool or not going to school.")
 
elif your_age >= 12 and your_age <= 14:

         print("Since the age submitted is" + " " +  str(your_age) + " " +  "you are probably in middle school.")
 
elif your_age >= 15 and your_age <= 18:

         print("Since the age submitted is" + " " +  str(your_age) + " " +  "you are probably in high school.")
 
elif your_age > 18

         print("Sorry, only children that were under 18 were asked about.")

Any questions? Do you see how the "and" operator makes more complex things than what was on the last conditionals tutorial by me? What are some other operators that can also spice things up? Have you used them or "and" in your programming? What difference did it make?
 
Last edited:
OK. There's a little to unpack here.

First up, the quotes really do matter - I know they seem oddly annoying but all the languages really care because they're too stupid to know when something is a string and when something isn't. In the same way that 500 and "500" are not the same thing either and you have to tell it when something is so.

So, as per the other thread, elseif isn't valid Python (it's ambiguous syntactically in other languages which is why Python straight up disallows it). The trailing else also needs a : and the first if has to be written in lowercase, If is not valid.

input() as we know from the other thread gives us a string and you can't compare strings to integers, so we need to tweak it a little.

We also can't write 'if your_age <= 11 and >=5:' because it will not stop to infer that you mean 'if your_age <= 11 and your_age >=5:' instead - it'll just go 'oh you forgot something here' because there's no requirement to have it be the same thing.

More importantly, when the language works it out, it *doesn't* think about it as though 'if your age is <= 11 and >= 5 therefore we're still talking about your age' - it wraps everything in brackets. What's really happening is:

if (your_age <= 11) and...
-> if not true, skip everything else, because if the first part isn't true, the second part doesn't matter
-> if true... we're now (true and (SOMETHING >= 5)) -> boom

That's why we don't write it just as English, because that relies on making a super huge assumption about context that a computer simply can't use.

Code:
your_age = int(input("What is your under 18 child's age?"))

if your_age <= 11 and your_age >= 5:

        print("Since the age submitted is " + str(your_age) + " you are probably in elementary school.")
        
elif your_age < 5:

        print("Since the age submitted is " + str(your_age) + " you are probably in preschool or not going to school.")
        
elif your_age >= 12 and your_age <= 14:

         print("Since the age submitted is " + str(your_age) + " you are probably in middle school.")
        
elif your_age >= 15 and your_age <= 18:

         print("Since the age submitted is " + str(your_age) + " you are probably in high school.")
        
else:

         print("Sorry, only children that were under 18 were asked about.")
 
OK. There's a little to unpack here.

First up, the quotes really do matter - I know they seem oddly annoying but all the languages really care because they're too stupid to know when something is a string and when something isn't. In the same way that 500 and "500" are not the same thing either and you have to tell it when something is so.

So, as per the other thread, elseif isn't valid Python (it's ambiguous syntactically in other languages which is why Python straight up disallows it). The trailing else also needs a : and the first if has to be written in lowercase, If is not valid.

input() as we know from the other thread gives us a string and you can't compare strings to integers, so we need to tweak it a little.

We also can't write 'if your_age <= 11 and >=5:' because it will not stop to infer that you mean 'if your_age <= 11 and your_age >=5:' instead - it'll just go 'oh you forgot something here' because there's no requirement to have it be the same thing.

More importantly, when the language works it out, it *doesn't* think about it as though 'if your age is <= 11 and >= 5 therefore we're still talking about your age' - it wraps everything in brackets. What's really happening is:

if (your_age <= 11) and...
-> if not true, skip everything else, because if the first part isn't true, the second part doesn't matter
-> if true... we're now (true and (SOMETHING >= 5)) -> boom

That's why we don't write it just as English, because that relies on making a super huge assumption about context that a computer simply can't use.

Code:
your_age = int(input("What is your under 18 child's age?"))

if your_age <= 11 and your_age >= 5:

        print("Since the age submitted is " + str(your_age) + " you are probably in elementary school.")
       
elif your_age < 5:

        print("Since the age submitted is " + str(your_age) + " you are probably in preschool or not going to school.")
       
elif your_age >= 12 and your_age <= 14:

         print("Since the age submitted is " + str(your_age) + " you are probably in middle school.")
       
elif your_age >= 15 and your_age <= 18:

         print("Since the age submitted is " + str(your_age) + " you are probably in high school.")
       
else:

         print("Sorry, only children that were under 18 were asked about.")
I see what I did wrong. I will correct. It's nothing major.
 

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