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