#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 9/16/2022 DS 2000 Lecture 3 - solidifying input/functions, reading/writing files, main() Logistics: - Homework 1 (due *today* @ 9pm) - because of the difficulties with Spyder installation/upgrade, there will be NO LATE PENALTY for HW 1 - style guide is on the course website - grading rubric is also on the course website - Homework 2 (released, available on the course website, due next Friday) 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 - // operator --- Question: What does the // operator do? A. it is the same as the / operator ("regular" calculator division) B. it is the same as the % operator (gives us the remainder) C. it takes the floor (always rounds down) of a division <-- D. it takes the ceiling (always rounds up) of a division E. it only works with integers """ print("warm up 1") num_cats = 2 num_years = 3 num_cats = num_cats + (num_years * 4) print(num_cats) print() """ Warm-up 1 - variable assignment --- Question: What is the value of the variable num_cats at the end of the following code snippet? num_cats = 2 num_years = 3 num_cats = num_cats + num_years * 4 A. 14 B. 20 C. 11 D. something else E. Error """ print("warm up 2") fave_num = int(input("what is your favorite number? ")) #fave_num = int(fave_num) print(type(fave_num)) """ Warm-up 2 - input() and type casting --- Question: What is the type of the variable fave_num when we call the type() function? fave_num = input("what is your favorite number? ") int(fave_num) print(type(fave_num)) A. int B. float C. str <--- but why???? D. something else E. Error """ """ review: vocabulary ---- - function: a pre-packaged group of code that does some job, often takes arguments/parameters, but doesn't need to be - argument/parameter: information that we give to functions (inputs) - return/return value: information that the function gives the program when it finishes running (output) - data type: int, string, float - how we are representing information on the computer, you can do different things with different types int + int str + str int + str -> Error int + float """ """ additions: vocabulary ---- - "call": "calling a function" -> the place in the code that we actually wrote down the function name and told it to execute "running a function" -> "where do you call that function?" - "pass in": "passing in parameters/arguments" -> "what did you pass in to your function?" -> what values did you give your function """ """ Computer organization: files, folders, home directories, oh my! --- directory <-> folder / root directory /Users /felix /Desktop ds2000notes.txt fluffycat.jpg ... /Documents /Applications ... Where does this file live? - lec3_starter_sec4.py - /Users/felix/Dropbox/ds2000/lectures/lec3_starter_sec4.py How do I know? - eagle eyes and look in the upper left - run the code and python will tell us With your neighbor: where is the file that you are currently editing located? Assumption: - you are working in an untitled file -> save that file somewhere you can find it - you are working in a titled file in some ds 2000 folder - you are working in a temp file - by default Spyder displays this when you start - the temp file is located in a directory called .spyder-py3 -> sace the file somewhere else ("save as") Prof. Felix's recommendations: -> always start by saving your file -> do make a ds 2000 directory/folder -> if you move files in Finder/File explorer, save & exit them in spyder first ---- """ """ Structuring our programs --- So far... everything was at the zero indentation level Now... def main(): # code main() """ """ Today's problem: Write a program, zoo.py, that represents a zoo that we're starting. First, this program should greet the user (by name, of course), then tell them what animals we have in our zoo. """ # see zoo.py """ Getting started with data ---- To read data from an external file, we need to make sure that we understand the file system on our computers. Importantly: - where does the python program that we're editing exist - where does the data file that we want to work with exist - ideally, these are in the same place When working with files, we do three things: 1. open the file 2. do stuff with the contents of the file 3. close the file """ # opening a file example with animals.txt # 1. open the file in "r"ead mode # Common error A: FileNotFound - "animal.txt" animal_file = open("animals.txt", "r") # 2. do stuff # common error: misspell readline line = animal_file.readline() print("line:", line) line = animal_file.readline() print("line:", line) # 3. close the file animal_file.close() """ Reading from a file and the "cursor" ---- When python opens a file (successfully), the cursor starts at the beginning of the file file.readline() function --- 1. Does this function give me any information back? 2. what is this function's name? 3. what information does this function need to run? """ # reading multiple lines from a file """ Today's problem, updated: Write a program, zoo.py, that represents a zoo that we're starting. First, this program should greet the user (by name, of course), then tell them what animals we have in our zoo. Read and display the information about what animals exist in the zoo from the animals.txt file. """ """ Summary: """