1 of 33

CS 110

If, else, and elif

Benjamin Dicken

2 of 33

3 of 33

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

4 of 33

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

5 of 33

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

6 of 33

statements . . .

if condition:

statement 1a

statement 2a

. . .

statement Na

else:

statement 1b

statement 2b

. . .

statement Nb

statements . . .

7 of 33

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

8 of 33

String comparison

  • Compares alphabetically, one character at-a-time, left-to-right

'albatross' == 'albertson'

What is the result?

9 of 33

String comparison

  • Compares alphabetically, one character at-a-time, left-to-right

'albatross' <= 'albertson'

What is the result?

10 of 33

String comparison

  • Compares alphabetically, one character at-a-time, left-to-right

'albatross' <= 'albERTSON'

What is the result?

11 of 33

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

12 of 33

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

13 of 33

elif

  • You can follow an if with zero or more elif statements
  • With elif statements, you provide a condition as you do with an if statement
  • A chain of if/elif will be followed until it reaches a condition that evaluates to True
  • One such a condition is reached, python will execute the body of that particular if or elif and then continue on after the chain.

14 of 33

statements . . .

if conditionA:

statements

elif conditionB:

statements

statements . . .

15 of 33

statements . . .

if conditionA:

statements

elif conditionB:

statements

elif conditionC:

statements

statements . . .

16 of 33

statements . . .

if conditionA:

statements

elif conditionB:

statements

elif conditionC:

statements

elif conditionD:

statements

statements . . .

17 of 33

statements . . .

if conditionA:

statements

elif conditionB:

statements

elif conditionC:

statements

else:

statements

statements . . .

18 of 33

Write the program

  • Accepts a number as input
  • Prints out the letter grade that you will receive

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

19 of 33

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

20 of 33

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

21 of 33

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

22 of 33

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

23 of 33

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

24 of 33

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

25 of 33

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

26 of 33

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

27 of 33

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

28 of 33

Blood Pressure

  • Blood pressure measurement machines at pharmacies
  • Some people don’t know how to properly interpret the numbers
  • Might be good for machine to indicate low, normal, or high

29 of 33

Blood Pressure

  • Systolic: Pressure on artery walls while ventricles in heart squeeze
  • Diastolic: Pressure on artery walls while heart relaxes (thus, lower)

  • Write code that allows user to enter two inputs (systolic pressure and diastolic pressure) and determines if it is low, normal, or high

Activity

30 of 33

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

31 of 33

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

32 of 33

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

33 of 33

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