#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 9/23/2022 DS 2000 Lecture 5 - (finish conditionals) while loops, random module Logistics: - Homework 2 due *tonight* @ 9pm - late deadline: Sunday @ 9pm - Quiz 3 goes out: *MONDAY @ 9AM* - expect all future quizzes to go out on Mondays @ 9am - Homework 3 is available *now* - after lecture today, you'll have all the tools that you need to complete the homework! - Homework 1 grades were released this morning - you have one week to submit regrade requests via Gradescope - this will be the case for all homeworks in general! - remote attendance (https://bit.ly/remote-ds2000-muzny) Three ways to participate (please do one of these!) 1) via the PollEverywhere website: https://pollev.com/muzny 2) via text: text "muzny" to the number 22333 to join the session 3) via Poll Everywhere app (available for iOS or Android) Warm-up 1 - string concatenation --- concatenation: adding two strings together Say you have the following code: # assume the user types "my_fancy_file" file = input("what is the file name? ") extension = ".txt" print(file + "extension") A. my_fancy_file.txt <-- if extension, instead of "extension" B. file.txt C. my_fancy_fileextension <--- correct, "extension" is a string D. fileextension E. Error Summary: - strings are values - strings are between quotes - variables store values - variable name does not affect what kind of values it stores - when python encounters a variable, it will look up its value (and plug it in to the code that is executing) """ """ Warm-up 2 - conditionals --- What is the output from the following code? """ bday = 23 if bday == 23: print("happy birthday day") else: # bday != 23: print("today is not your birthday day") """ A. happy birthday day B. happy birthday day then today is not your birthday day C. today is not your birthday day D. nothing E. Error <--- correct, we can't put a boolean exp after else """ """ Conditionals: what we rememember --- - incorporate boolean expressions which evaluate to True or False - boolean expressions go after if (or elif) - control program flow by adding branches - else: covers all cases not tested for - elif: we will talk about this in a moment """ """ Advanced conditionals: if/elif/else --- A conditional with just an if if boolean exp: # code 1 A conditional with an if/else if boolean exp: # code 1 else: # code 2 A conditional with an if/elif/else if boolean exp: # code 1 elif boolean exp: # code 2 else: # code 3 """ print("if/elif/else example") print("only ONE branch will execute") x = 30 # if test is ALWAYS run if x > 75: print("so big!") # if test was False if this test actually gets evaluated elif x > 50: # and x <= 75 print("so medium!") # if test and first elif were False if this test actually gets evaluated elif x > 25: # and x <= 50 print("so medium-small!") else: print("so small!") print("all done") print() """ Advanced conditionals: if-then-if --- TWO control structures/TWO separate conditionals Every if starts a new conditional, every if's boolean exp ALWAYS gets tested: if/elif/else: when we want a MAXIMUM of one branch to execute (and one must execute) if/elif: when we want a MAXIMUM of one branch to execute (and a MINIMUM zero) if/if: when we want a MAXIMUM of twos branches to execute (and a MINIMUM zero) """ print("if/elif/else example 2") x = 80 if x > 75: print("so big!") if x > 50: print("so medium!") print("all done") print() """ General conditionals syntax: how many of what? --- if exp: # ALWAYS 1 # code elif exp: 0 -> as many as we want # code else: # 0 or 1 # code """ """ Control structures ----- When we learned about conditionals, we learned about a *control structure*. This is python code that dictates what code should be executed next... """ """ random module --- A library to generate random numbers """ # import random module import random # generate a random int [1, 10] (inclusive) num = random.randint(1, 10) print("random num:", num) """ Example problem: --- Write a program, hotcold.py, that randomly picks a number between 1 and 10 then has the user guess a number. If the user is correct, congratulate them. If the user is too high, tell them "too high!". If the user is too low, tell them "too low!" """ """ Repetition --- On Tuesday, we talked about how important it is for our programs to be able to do all of the following things: 1) Memory 2) Re-using code 3) Branching 4) Repetition <-- today """ """ While loops --- Conditionals, but if you gave them jet engines! while boolean exp: # code you want repeated The code will be repeated (run again and again) until the boolean exp. becomes false """ # examples of while loops print("while 1") # 1) initialize the important values count = 1 # 2) test the tracker variable to see # if we should keep repeating while count <= 5: print(count) # 3) update the variable we're testing! count = count + 1 print("while done") print() """ An unfortunate discovery --- What is infinity and what does it look like in python? A while loop whose test condition never becomes False """ # be very careful with the code below! # examples of while loops print("while 2") # 1) initialize the important values count = 1 # 2) test the tracker variable to see # if we should keep repeating # if we have a bug in our test we might discover infinity while count <= 5: print(count) # 3) update the variable we're testing! # if we forget to do this we'll discover infinity count = count + 1 print("while done") print() """ Example problem (updated): --- Write a program, hotcold.py, that randomly picks a number between 1 and 10 then has the user guess a number. If the user is correct, congratulate them *and stop the program*. If the user is incorrect, prompt them to guess again. Each time the user guesses tell them "warmer" if they are getting closer to the secret number and "colder" if they are getting farther away. """ # We'll start with this on 9/27! """ While loops + plotting ---- We can *absolutely* combine plotting with while loops. For instance, let's plot the guesses that the user was making in our hotcold.py program. """