Chapter 5: IF Statements Flashcards

1
Q

print out a list of cars using a for loop, but single out a specific value to add a function to it

A
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
      if car == 'bmw':
          print(car.upper())
      else:
           print(car.title())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

write several simple conditional tests, some being True and some being False

A

car = ‘bmw’
print(car == ‘bmw’)

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

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

write several simple conditional tests while changing capitalizations

A

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

print(car.lower() == ‘audi’)

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

us an if statement and a variable to check for inequality

A

requested_topping = ‘mushrooms’

if requested_topping != ‘mushrooms’:
print(“hold the anchovies”)

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

check whether a number is the same as a number in a variable

A

age = 18

print(age == 18)

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

use an if statement to check a numbered variable for inequality

A

answer = 17

if answer != 17:
print(“string”)

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

make other numerical comparisons using other signs

A

< > <= >=

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

write a numerical comparison that needs two conditions to be met to be true or false

A
age = 22
age_1 = 18
print(age >= 21 and age_1 >= 21)
age_1 = 22
print(age >= 21 and age_1 >= 21)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

create a list in a variable and check if a value is in a list
write one that is true and one that is false

A

requested_topping = [‘mushrooms’, ‘onions’, ‘pineapple’]

print(‘mushrooms’ in requested_topping)

print(‘pepperoni’ in requested_topping)

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

create a list in a variable and create a variable with a value that will be checked to see if it is not in the list using an if statement

use an f-string in the if statement response

A
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'

if user not in banned_users:
print(f”{user.title()}, you can post a response if you wish”)

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

what is a boolean expression

A

a conditional test uses boolean values that are either true or false

boolean values provide an efficient way to track the state of a program or a particular condition that is important in your program

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

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(“Is car == ‘subaru’? I predict True.”)
print(car == ‘audi’)

print(“\nIs car == ‘subaru’? I predict False.”)
print(car == ‘audi’)

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

write a simple if statement

then use a variable to do the same thing

A

if conditional_test:
print(do something)

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
14
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
15
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
16
Q

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

A

elif age<65:
price = 40
else:
price = 20

17
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

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

19
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.

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

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

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