#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 9/27/2022 DS 2000 Lecture 6 - while loops (finish), intro to for loops, looping through files Logistics: - Quiz 3 is *already out* - due on Friday at 9:50am - Homework 3 is already available - again, you already have all the tools that you need for this homework — we do not expect you to use any of the material from this week on HW 3 - no need to fill out a remote attendance form. :) 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 0 - how are you doing? --- A. great! B. tired! C. overwhelmed! D. curious! E. something else! Warm-up 1 - while loops 1 --- What will happen with the following while loop? count = 10 while count < 10: print(count) count = count + 1 A. prints 1 - 10 B. prints 10 C. prints 10 forever D. Nothing E. Error Warm-up 2 - while loops 2 --- Fill in the following code so that the while loop prompts the user for an answer until they type "end" """ """ Fun tid-bits --- In python, we have a shorthand for writing down: x = x + 1 Instead, we write: x += 1 Similarly, x = x + 5 becomes x += 5. We can also do this with the - operator: x = x - 1 Becomes: x -= 1 """ """ While loops: what we rememember --- """ """ 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. We'll save this program as hotcold_plots.py """ """ While loops: common patterns --- (https://muzny.github.io/csci1200-notes/06/2/common_while_uses.html) (https://muzny.github.io/csci1200-notes/06/3/while_accumulator.html) "until X" "up to a number/repeat X times" "accumulator" """ # ask the user if they like cats until they say yes # print out a random number from 1 to 3 five times # count the number of 1s in five random numbers from 1 to 3 """ Nested loops: what's the story --- Typically used when you have a task that needs repeating and the *task itself* involves repetitions. For example, suppose that I want to count from 1 - 3 ten times. inner loop: outer loop: """ # example, note that both loops have all of the three # required parts of a while loop. """ Reading files with while loops 1 --- While loops let us repeat an action as many times as we'd like to. Take a look at our first data file for today: - distances.txt It's not immediately obvious what we would want the condition on our while loop to be if we used a loop to go through this file... """ # file = open("distances.txt", "r") # while _____: # line = file.readline() # file.close() """ Data structures: lists ----- """ """ Getting a list from a string: str.split() ----- """ """ Reading files with while loops 2 --- Getting a list of strings out of the file: """ """ Example problem: --- Write a program, distance.py, that reads the data from the file distances.txt and graphs it accordingly on a scatter plot. """ """ for loops 1: syntax --- """ """ General for loops syntax 1 --- """ """ Example problem, updated: --- Write a program, distance.py, that reads the data from the file distances_complex.txt and graphs it accordingly on a scatter plot. Color days that were a run with one color and days that were a bike ride a different color. """ """ Next time: - more for loops! - for loops with indexes - all the ways to iterate over a list """