# Loops --- CS 65 // 2021-03-04 ## Extra Credit Activities - MathCS Seminar (Dr. Enes Akbuga) + **Topic:** Mathematics Education + **When:** Friday, 2:00pm to 3:00pm + **Where:** Zoom (email me for the link) - [Global Citizen Forum](https://www.drake.edu/diversity/initiatives/globalcitizenforum2021/) + Conference on equity and inclusion in higher ed + March 3-5 ## Extra Credit Activities - MathCS Special Interest Group + Microsoft Team to help foster community between the students and faculty interested in topics related to Mathematics and Computer Science # Assignment 5 - Will be posted later today - It is a **group** assignment - Your partner today is your partner for the assignment # Questions ## ...about anything? # `fizzbuzz` ## `fizzbuzz` - Write a program called `fizzbuzz.py` that asks the user to input a number `num` and outputs the following. + Outputs `"fizz"` if `num` is divisible by 3, + Outputs `"buzz"` if `num` is divisible by 5, + Outputs `"fizzbuzz"` if `num` is divisible by both 3 and 5, and + Outputs `num` otherwise. ## Is `num` divisible by 3? - How can we check this? - `if num % 3 == 0:` ## Attempt 1 ```py num = float(input("Enter a number: ")) if num % 3 == 0: if num % 5 == 0: print("fizzbuzz") else: print("fizz") elif num % 5 == 0: print("buzz") else: print(num) ``` - This works...but what about adding a case for if `num` is divisible by 7? ## It gets terribly messy... ```py num = float(input("Enter a number: ")) if num % 3 == 0: if num % 5 == 0: if num % 7 == 0: print("fizzbuzzjazz") else: print("fizzbuzz") elif num % 7 == 0: print("fizzjazz") else: print("fizz") elif num % 5 == 0: if num % 7: print("buzzjazz") else: print("buzz") elif num % 7 == 0: print("jazz") else: print(num) ``` ## Attempt 2 ```py num = float(input("Enter a number: ")) output = "" if num % 3 == 0: output = output + "fizz" if num % 5 == 0: output = output + "buzz" if num % 7 == 0: output = output + "jazz" if output != "": print(output) else: print(num) ``` # Repetition ## Repetition - It is often the case when we need to perform a similar operation **repeatedly** - Before today, we've seen an approach to perform repetition. What is it? + `for` loop ## For Loop - We know we can use `for` loops to do something a many times
# Calls the do_something function 10 times for i in range(10): do_something()
- Next week we'll see that they can do even more ## While Loops - A **while loop** in Python can be specified in the following way ```py while boolean_expression: statement1 statement2 ... ``` + It executes its body of statements repeatedly as long as the condition remains **True** ## Example - Consider the following program ```py print("This program is designed to cheer you up!") keep_going = "yes" while keep_going == "yes": print("YOU ARE AWESOME! YOU ARE THE BEST!") keep_going = input("Do you need more cheers? (yes/no): ") print("Hope you're cheered up!") ``` ## Self Check 1 - Let's take a look at the following program ```py x = 5 while x < 100: print(x) x = 2*x - 1 print(-1) ``` - What does this do? What will be printed? ## Hailstone with a While Loop ```py def hailstone(n): if n % 2 == 0: return n//2 else: return 3*n + 1 ``` ```py def iterate_hailstone(n): while n > 1: n = hailstone(n) print(n) print(n) # prints 1 at the end ``` ## `break` - It is possible to immediately exit a `while` loop (even if the condition still holds) by using the `break` statement - `break` can be convenient, but it can lead to code that is difficult to read - Whenever you are tempted to use a `break` statement, consider changing the condition of your `while` loop instead ## Self Check 2 - Consider the following programs and determine whether they are functionally equivalent: ```py while True: line = input('> ') if line == 'done': break print(line) print('Done!') ``` ```py line = input('> ') while line != "done": print(line) line = input('> ') print('Done!') ``` # Lab Time