Python for Absolute Beginners Transcripts
Chapter: Code that interacts with users
Lecture: Concept: Multiple conditions to test at once
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We've seen if statements and we've seen wild loops, and we've also seen truthiness. But, what if you have two different conditions
0:08
you have to test at the same time? So imagine you're writing a website and it's an e-commerce site. People have a shopping cart
0:15
they can put stuff in the shopping cart, and then later somewhere you can tell them hey so and so, you have this many items in your cart.
0:22
Well there's a problem you can really only have a shopping cart if the user is logged in. Because if they're not logged in
0:29
where do you put their cart info? So we want to check both, that the user is logged in and that they have items in their account.
0:36
So if they don't have either of those then we are going to do some else statements. So we'll say you have some number of items in your cart
0:43
so and so, Michael Thor, log in and add some items to your cart. So we're going to get the user account
0:47
which is a number, and we are going to get the user which is an object. It can either point at something or it could be none.
0:53
Those are the two basic values you can have here. So we're going to test both of them. One, item count greater than zero.
1:01
If we were sure it wouldn't be less than zero for some weird reason. We could just say item count with that truthiness table
1:06
but lets be a little bit explicit here. Then we also want to check that the user is not missing. So we can just say user
1:12
and then we combine these with various words the yellow here. We say and this means we want to check both, that they have items in the shopping cart
1:20
and they're logged in. If it was sufficient that either of these were true then we could say item count or user right?
1:27
It might just say you have zero items in your cart Michael, if your cart is empty, but you're logged in. Alright so you can use and, and or
1:33
to combine these various tests into more complicated ones like this.