if statements Flashcards
5.1 Write a series of conditional tests. Print a statement describing each test and your prediction for the results of each test
car = ‘subaru’
print(car == ‘subaru’)
print(car == ‘audi’)
Write an if statement with one test and one action
if conditional_test:
do something
or
age = 19
if age >= 18:
print(“you are old enough to vote!”)
add an else statement to any previous if statement
age = 19
if age >= 18:
print(““you are old enough to vote!””)
Else:
print(“You are too young to vote”)
write an if-elif-else chain using -admissions for anyone under the age of 4 is free - admissions for anyone between the ages of 4 and 18 is $25 -admissions for anyone older than 18 is $40
age = 12 if age < 4: price = 0 elif age <18: price = 25 else: price = 40
add another elif statement with the additions of above 65 = 40.
elif age<65:
price = 20
why would you omit the else statement?
if you have a specific final condition you are testing for, it may be appropriate to omit the else
test multiple if statements using a list
requested_toppings = [‘mushrooms’, ‘extra cheese’]
If ‘mushrooms’ in requested_toppings:
print(“adding mushrooms”)
if ‘pepperoni’ in requested_toppings:
print(“adding pepperoni”)
if ‘extra cheese’ in requested_topping:
print(“adding extra cheese”)
why would you use multiple if statements?
python only needs one test to pass in an if-elif-else chain. Use multiple if it’s possible for more than one condition to be true.
write a for loop that prints out a value in a string
requested_toppings = [‘mushrooms’, ‘green peppers’, ‘extra cheese’]
for value in requested_toppings:
print(f”“adding {value}.””)
print(“\nFinished making your pizza”)
add an if-else chain to the for loop that says they are out of green peppers
if value == ‘green peppers’:
print(““sorry we are out of green peppers””)
else:
print(f”adding {value}”)
Check to make sure a list isn’t empty
if requested_toppings:
for value in requested_toppings:
print(f”adding {value}.”)
print(“\nFinished making your pizza”)
else:
print(“are you sure you want a plain pizza”)
Use the pizza example to create multiple lists and check if the items in one list are in the other list . use if-else statement to print out answers if an item is and isn’t in a list
available_toppings = [‘mushrooms’, ‘olives’, ‘pepperoni’, ‘pineapple’,’extra cheese’, ‘green peppers’]
requested_toppings = [‘mushrooms’, ‘french fries’, ‘extra cheese’]
for value in requested_toppings:
if value in available_toppings:
print(f”adding {value} to the pizza.”)
else:
print(f”sorry we don’t have any {value}”)
- 8: make a list of five or more usernames, including the name ‘admin’. imagine you are writing code that will print agreeing to each user after they log into a website. loop through the list and print a greeting to each.
- if the user is ‘admin’, print a special greeting
- otherwise print a generic greeting
usernames = [‘eric’, ‘willie’, ‘admin’, ‘erin’, ‘ever’]
for username in usernames:
if username == ‘admin’:
print(“Hello admin, would you like to see a status report?”)
else:
print(f”Hello {username}, thank you for loggin in again!”)
- 9: add an if test to make sure to 5.8 to make sure the list is not empty
- if the list is empty, print ‘we need to fid some users’
- finally, clear the list and then make sure the correct message is printed
usernames = []
if usernames: for username in usernames: if username == 'admin': print("Hello admin, would you like to see a status report?") else: print(f"Hello {username}, thank you for loggin in again!") else: print("We need to find some users!")
5.10: Do the following to create a program that simulates how websites ensure that everyone has a unique username.
- Make a list of five or more usernames called current_users.
- Make another list of five usernames called new_users.
- Make sure one or two of the new usernames are also in the current_users list.
- Loop through the new_users list to see if each new username has already been used.
- If it has, print a message that the person will need to enter a new username.
- If a username has not been used, print a message saying that the username is available.
- Make sure your comparison is case insensitive. If ‘John’ has been used, ‘JOHN’ should not be accepted.
current_users = ['eric', 'willie', 'admin', 'erin', 'Ever'] new_users = ['sarah', 'Willie', 'PHIL', 'ever', 'Iona']
current_users_lower = [user.lower() for user in current_users]
for new_user in new_users:
if new_user.lower() in current_users_lower:
print(f”Sorry {new_user}, that name is taken.”)
else:
print(f”Great, {new_user} is still available.”)