lec-13
# Complete wall_dimensions_from_user
# Complete paint_available_from_user
# Make a bool-returning function, enough_paint
def main() -> None:
wall_width = wall_dimension_from_user("width")
wall_height = wall_dimension_from_user("height")
wall_area = wall_width * wall_height
paint_avail = paint_available_from_user()
# Report whether enough paint is available to
# complete painting the given wall, using
# enough_paint function.
#
# Assume 1L of paint will cover 6 square metres
# of wall.⦾ looping
⦾ the while loop
⦾ input validation
⦾ tracing while loops
read from an input device
write to an output device
read a value from storage
write a value to storage
"do math"
branch
loop (repeated branching)
read from an input device
write to an output device
read a value from storage
write a value to storage
"do math"
branch
loop (repeated branching)
99 bottles of age-appropriate beverages on the wall,
99 bottles of age-appropriate beverages,
take one down, pass it around,
98 bottles of age-appropriate beverages on the wall.
98 bottles of age-appropriate beverages on the wall,
98 bottles of age-appropriate beverages,
take one down, pass it around,
97 bottles of age-appropriate beverages on the wall.
97 bottles of age-appropriate beverages on the wall,
97 bottles of age-appropriate beverages,
take one down, pass it around,
96 bottles of age-appropriate beverages on the wall.
...
I just had to change this...
it sounded so wrong otherwise!
let number_of_bottles = 99 while number_of_bottles is greater than 2 print "[number_of_bottles] bottles of age-appropriate beverages on the wall,"
print "[number_of_bottles] bottles of age-appropriate beverages," print "take one down, pass it around," let number_of_bottles = number_of_bottles - 1 print "[number_of_bottles] bottles of age-appropriate beverages on the wall,"
print "2 bottles of age-appropriate beverages on the wall," print "2 bottles of age-appropriate beverages," print "take one down, pass it around," print "1 bottle of age-appropriate beverages on the wall,"
print "1 bottle of age-appropriate beverages on the wall," print "1 bottle of age-appropriate beverages," print "Nah nah nah nah nah nah nah nah nah"...
def sing_that_song():
number_of_bottles = 99
while number_of_bottles > 2:
print(f"{number_of_bottles} bottles of age-appropriate beverages on the wall,")
print(f"{number_of_bottles} bottles of age-appropriate beverages,")
print("take one down, pass it around,")
number_of_bottles = number_of_bottles - 1
print(f"{number_of_bottles} bottles of age-appropriate beverages on the wall,")
print()
print("2 bottles of age-appropriate beverages on the wall,")
print("2 bottles of age-appropriate beverages,")
print("take one down, pass it around,")
print("1 bottle of age-appropriate beverages on the wall,")
print()
print("1 bottle of age-appropriate beverages on the wall,")
print("1 bottle of age-appropriate beverages,")
print("Nah nah nah nah nah nah nah nah nah...")
sing_that_song()I am giddy with excitement and age-appropriate beverages!
🙋🏻♂️❓🙋🏻♀️Why no loop on the last 2 stanzas?
number_of_bottles = 99
while number_of_bottles > 2:
print(f"{number_of_bottles} bottles ...")
print(f"{number_of_bottles} bottles ...")
print("take one down, pass it around,")
number_of_bottles = number_of_bottles - 1
print(f"{number_of_bottles} bottles ...")
print()You need a while to have a while loop!
You need a condition to determine what keeps the loop going.
The condition uses a variable - the LCV (loop control variable).
The LCV should be given a useful initial value.
The LCV should be changed inside the loop.
You need one or more statements to run while your loop is going. (the loop body)
They all are important!
number_of_bottles = 99
while number_of_bottles > 2:
print(f"{number_of_bottles} bottles ...")
print(f"{number_of_bottles} bottles ...")
print("take one down, pass it around,")
number_of_bottles = number_of_bottles - 1
print(f"{number_of_bottles} bottles ...")
print()For some reason, it can be easier to think "When should I NOT loop?" and then FLIP that condition.
For this code, we DON'T want to loop when we have 1 or 2 bottles - that's to say, when number_of_bottles <= 2. So flip that.
TASK
Make a function that asks how many pets you have.
If the number entered is less than 0, re-prompt and ask again;
otherwise, return the number.
Assume (for now) an integer is entered.
TASK
Make a function that asks how many pets you have. If the number entered is less than 0, re-prompt and ask again; otherwise, return the number.
Assume (for now) an integer is entered.
def num_pets_from_user() -> int:
return -1Sure, this isn't necessary -
but you have to admit, it's kinda helpful...
I tend to put the return in right away, just so I don't forget. And I choose an invalid number, because it's useful for automated testing - but you'll have to just take my word for that.
I was super close to calling this valid_num_pets_from_user...but that might be a bit long.
Hard to tell!
🙋🏻♂️❓🙋🏻♀️What category of function is this? What's the other?
TASK
Make a function that asks how many pets you have. If the number entered is less than 0, re-prompt and ask again; otherwise, return the number.
Assume (for now) an integer is entered.
def num_pets_from_user() -> int:
num_pets = int(input("How many pets? "))
return -1Remember, we want to eventually return an int!
Naming things expressively becomes something you can do on autopilot here.
Don't forget spaces after prompts!
Attention to detail is a habit we try to cultivate.
This initial asking of input from the user is called a priming read.
Yes, you should memorize that term.
🙋🏻♂️❓🙋🏻♀️What do we call this process of turning something of one type into another?
TASK
Make a function that asks how many pets you have. If the number entered is less than 0, re-prompt and ask again; otherwise, return the number.
Assume (for now) an integer is entered.
def num_pets_from_user() -> int:
num_pets = int(input("How many pets? "))
while num_pets < 0:
print("Num of pets must be >=0.")
num_pets = int(input("How many pets? "))
return -1We can print a "naughty-naughty" warning if we want to. It's usually friendlier to do so than not.
See if there is invalid input - if so, we've got work to do!
Remember to get their new response! And note how this line is IDENTICAL to our priming read...
Remember:
It can be easier to ask "when should I NOT loop?" and then flip it.
TASK
Make a function that asks how many pets you have. If the number entered is less than 0, re-prompt and ask again; otherwise, return the number.
Assume (for now) an integer is entered.
def num_pets_from_user() -> int:
num_pets = int(input("How many pets? "))
while num_pets < 0:
print("Num of pets must be >=0.")
num_pets = int(input("How many pets? "))
return num_petsTASK
Make a function that asks how many pets you have. If the number entered is less than 0, re-prompt and ask again; otherwise, return the number.
Assume (for now) an integer is entered.
def num_pets_from_user() -> int:
num_pets = int(input("How many pets? "))
while num_pets < 0:
print("Num of pets must be >=0.")
num_pets = int(input("How many pets? "))
return num_petsYou need a while to have a while loop!
You need a condition to determine what keeps the loop going.
The condition uses a variable - the LCV (loop control variable).
The LCV should be given a useful initial value.
The LCV should be changed inside the loop.
You need one or more statements to run while your loop is going. (the loop body)
TASK
Change the function so that now we DON'T assume the user enters an integer - now we want to see if it's an integer AS WELL AS >= 0
Hint: the isdigit() method can be used with strings. It returns True iff every character in the string is a digit. For example:
"12".isdigit() is True
"hi!".isdigit() is False
"5.12".isdigit() is False
"-2".isdigit() is False
def num_pets_from_user() -> int:
num_pets = int(input("How many pets? "))
while num_pets < 0:
print("Num of pets must be >=0.")
num_pets = int(input("How many pets? "))
return num_petsHONOURS_CUTOFF = 3.6
PASS_CUTOFF = 2.0
def display_grade_stats(num_students: int) -> None:
student_count = 0
num_honours = 0
num_passed = 0
num_failed = 0
while student_count < num_students:
gpa = float(input(f"Student {student_count + 1} GPA: "))
if gpa >= HONOURS_CUTOFF:
num_honours += 1
elif gpa >= PASS_CUTOFF:
num_passed += 1
else:
num_failed += 1
student_count += 1
print(f'''h: {num_honours} p: {num_passed} f: {num_failed}''')
# Call for 3 students, entering 3, 4, 1
display_grade_stats(3)
New thing
augmented assignment operator
🙋🏻♂️❓🙋🏻♀️What dangerous thing is this function doing?
🙋🏻♂️❓🙋🏻♀️There's a subtle logic error here. See it?
Remember:
It can be easier to ask "when should I NOT loop?" and then flip it.