if statements Flashcards

1
Q

5.1 Write a series of conditional tests. Print a statement describing each test and your prediction for the results of each test

A

car = ‘subaru’
print(car == ‘subaru’)
print(car == ‘audi’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write an if statement with one test and one action

A

if conditional_test:
do something

or

age = 19
if age >= 18:
print(“you are old enough to vote!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

add an else statement to any previous if statement

A

age = 19
if age >= 18:
print(““you are old enough to vote!””)
Else:
print(“You are too young to vote”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A
age = 12
if age < 4: 
     price = 0
elif age <18:
     price = 25 
else:
      price = 40
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

add another elif statement with the additions of above 65 = 40.

A

elif age<65:

price = 20

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

why would you omit the else statement?

A

if you have a specific final condition you are testing for, it may be appropriate to omit the else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

test multiple if statements using a list

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

why would you use multiple if statements?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

write a for loop that prints out a value in a string

A

requested_toppings = [‘mushrooms’, ‘green peppers’, ‘extra cheese’]
for value in requested_toppings:
print(f”“adding {value}.””)
print(“\nFinished making your pizza”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

add an if-else chain to the for loop that says they are out of green peppers

A

if value == ‘green peppers’:
print(““sorry we are out of green peppers””)
else:
print(f”adding {value}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Check to make sure a list isn’t empty

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

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}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 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
A

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!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. 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
A

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!")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.
A
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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

5.11:Ordinal numbers indicate their position in a list, such as 1st or 2nd. Most ordinal numbers end in th, except 1, 2, and 3.

Store the numbers 1 through 9 in a list.
Loop through the list.
Use an if-elif-else chain inside the loop to print the proper ordinal ending for each number. Your output should read “1st 2nd 3rd 4th 5th 6th 7th 8th 9th”, and each result should be on a separate line.

A

numbers = list(range(1,10))

for number in numbers:
    if number == 1:
        print("1st")
    elif number == 2:
        print("2nd")
    elif number == 3:
        print("3rd")
    else:
        print(f"{number}th")