2.6. if Statement with List
Checking for Special Items
Combining lists and if statements can be very beneficial. Let's see an example of ordering alcohol from a restaurant. This is just a simple for loop we learned in the previous section.
However, here, you want to check whether the age is appropriate to order alcohol. Say we cannot serve a kid a beer. We can use if statement inside the for loop.
This time we checked whether age < 21 and item == "beer" is True. Since this is True, we say that this order is not added.
Using Multiple Lists
Supposed the restaurant serves more than one alcoholic drink. In this case, we can use multiple lists in our if. We add an extra list of alcoholic drink.
Exercise 2.6
Car Dealer
If someone's saving is more than 25000 and their choice is in the stock, then it is possible to buy the car.
From the provided lists, try to print whether someone can buy a car or not. Here's an example of the output:
Client Name : Andy
Client Fund : Insufficient
Client Choice : Available
Possible Buy : False
Client Name : Bobby
Client Fund : Sufficient
Client Choice : Unavailable
Possible Buy : False
Client Name : Charlie
Client Fund : Sufficient
Client Choice : Available
Possible Buy : True
A part of the for loop are already done for you.Ordinal Numbers
Ordinal numbers indicate their position in a list, such as 1st, 2nd, or 3rd. Most ordinal numbers end in th, except 1, 2, and 3.Store the numbers 1 through 10 in a list.
Use an if-elif-else chain inside the loop to print the proper ordinal ending for each number. Your output should be
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th