CS 110
If, else, and elif
Benjamin Dicken
Harvard Study ( See: https://phys.org/news/2019-09-students-classrooms.html )
if-else
What pair of inputs would cause only "Workload OK" to print ?
title = input('Job title: ')
hours = int(input('Hours worked per week: '))
if title != 'manager' and hours > 20:
if title == 'waiter':
print('Workload too high')
else:
print('Workload OK')
if hours > 40:
print('Reduce hours')
else:
print('Acceptable hours')
Activity
if-else
Possible to print both "Workload OK" and "Reduce hours" ?
title = input('Job title: ')
hours = int(input('Hours worked per week: '))
if title != 'manager' and hours > 20:
if title == 'waiter':
print('Workload too high')
else:
print('Workload OK')
if hours > 40:
print('Reduce hours')
else:
print('Acceptable hours')
Activity
if-else
Use the if statement to execute code in the True case
Can add an else to specify code to run in the False case
statements . . .
if condition:
statement 1a
statement 2a
. . .
statement Na
else:
statement 1b
statement 2b
. . .
statement Nb
statements . . .
What are all of the inputs that will cause only “No need to retire yet” to print?
age = int(input('Enter age:'))
if age > 18:
if age > 65:
print('Are you retired yet?')
else:
print('No need to retire yet.')
if age < 25:
print('Why retire?')
else:
print('Wait until you are an adult')
Activity
String comparison
'albatross' == 'albertson'
What is the result?
String comparison
'albatross' <= 'albertson'
What is the result?
String comparison
'albatross' <= 'albERTSON'
What is the result?
Provide one pair of inputs to print “Workload too high”
title = input('Job title: ')
hours = int(input('Hours worked per week: '))
if title >= 'chair' and title <= 'wait':
if title != 'busser':
print('Workload too high')
else:
print('Workload OK')
if hours > 50:
print('Reduce hours')
else:
print('Acceptable hours')
Activity
if-else
Provide one pair of inputs that would cause “Not acceptable” to print out
age = int(input('Enter age:'))
citizenship = input('Enter citizenship:'))
if citizenship < 'i' and (age >= 15 or citizenship > 't'):
print('You can join the club')
else:
print('Not acceptable')
Activity
elif
statements . . .
if conditionA:
statements
elif conditionB:
statements
statements . . .
statements . . .
if conditionA:
statements
elif conditionB:
statements
elif conditionC:
statements
statements . . .
statements . . .
if conditionA:
statements
elif conditionB:
statements
elif conditionC:
statements
elif conditionD:
statements
statements . . .
statements . . .
if conditionA:
statements
elif conditionB:
statements
elif conditionC:
statements
else:
statements
statements . . .
Write the program
What is your numeric grade? 97
You will get an A
What is your numeric grade? 72
You will get a C
What is your numeric grade? 50
You will get an F
Activity
import os
numeric_grade = input('What is your numeric grade? ')
if numeric_grade.isnumeric() == False:
print('Enter a different value next time. Exiting.')
os._exit(0)
numeric_grade = int(numeric_grade)
# Write your code as if it would go here
if numeric_grade >= 60:
if numeric_grade >= 70:
if numeric_grade >= 80:
if numeric_grade >= 90:
print('You will get an A')
os._exit(0)
print('You will get a B')
os._exit(0)
print('You will get a C')
os._exit(0)
print('You will get a D')
os._exit(0)
print('You will get an F')
if numeric_grade >= 90:
print ('You will get an A')
if numeric_grade >= 80:
print ('You will get a B')
if numeric_grade >= 70:
print ('You will get a C')
if numeric_grade >= 60:
print ('You will get a D')
if numeric_grade < 60:
print ('You will get an F')
if numeric_grade >= 90:
print ('You will get an A')
elif numeric_grade >= 80:
print ('You will get a B')
elif numeric_grade >= 70:
print ('You will get a C')
elif numeric_grade >= 60:
print ('You will get a D')
elif numeric_grade < 60:
print ('You will get an F')
if numeric_grade >= 90:
print ('You will get an A')
elif numeric_grade >= 80:
print ('You will get a B')
elif numeric_grade >= 70:
print ('You will get a C')
elif numeric_grade >= 60:
print ('You will get a D')
else:
print ('You will get an F')
What will it print?
name = input('What is your name (lower case)? ')
if name <= 'chase' or name > 'trevor':
print('name category 1')
elif name > 'chase' and name < 'ianto':
print('name category 2')
else:
print('name category 3')
Activity
What will it print?
When the input is jeff ?
name = input('What is your name (lower case)? ')
if name <= 'chase' or name > 'trevor':
print('name category 1')
elif name > 'chase' and name < 'ianto':
print('name category 2')
else:
print('name category 3')
Activity
What will it print?
When the input is chance ?
name = input('What is your name (lower case)? ')
if name <= 'chase' or name > 'trevor':
print('name category 1')
elif name > 'chase' and name < 'ianto':
print('name category 2')
else:
print('name category 3')
Activity
What will it print?
When the input is iancu ?
name = input('What is your name (lower case)? ')
if name <= 'chase' or name > 'trevor':
print('name category 1')
elif name > 'chase' and name < 'ianto':
print('name category 2')
else:
print('name category 3')
Activity
Blood Pressure
Blood Pressure
Activity
Is this correct?
systolic = float(input('Systolic pressure: '))
diastolic = float(input('Diastolic pressure: '))
if systolic > 120:
if diastolic > 80:
print('High blood pressure')
elif systolic > 90:
if diastolic > 60:
print('Normal blood pressure')
else:
print('Low blood pressure')
Is this correct?
systolic = float(input('Systolic pressure: '))
diastolic = float(input('Diastolic pressure: '))
if systolic > 120 and diastolic > 80:
print('High blood pressure')
if systolic > 90 and diastolic > 60:
print('Normal blood pressure')
if systolic > 0 and diastolic > 0:
print('Low blood pressure')
Is this correct?
systolic = float(input('Systolic pressure: '))
diastolic = float(input('Diastolic pressure: '))
if systolic > 120 and diastolic > 80:
print('High blood pressure')
elif systolic > 90 and diastolic > 60:
print('Normal blood pressure')
else:
print('Low blood pressure')
Is this correct?
systolic = float(input('Systolic pressure: '))
diastolic = float(input('Diastolic pressure: '))
if systolic > 120 or diastolic > 80:
print('High blood pressure')
elif systolic > 90 or diastolic > 60:
print('Normal blood pressure')
else:
print('Low blood pressure')