CSc 110, Autumn 2017 Lecture 18: While loops and File Input

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Building Java Programs
Building Java Programs Chapter 5
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
1 BUILDING JAVA PROGRAMS CHAPTER 6 FILE PROCESSING.
BUILDING JAVA PROGRAMS CHAPTER 6 File Processing.
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1 – 6.2, 5.4.
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
1 Building Java Programs Chapter 5 Lecture 11: Random Numbers reading: 5.1, 5.6.
Building Java Programs
CSc 110, Autumn 2017 Lecture 15: Strings and Fencepost Loops
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
CSc 110, Spring 2017 Lecture 12: Random Numbers
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
Writing & reading txt files with python 3
CSc 110, Autumn 2017 Lecture 20: Line-Based File Input
Building Java Programs
Building Java Programs Chapter 5
Building Java Programs
CSc 110, Spring 2017 Lecture 18: Line-Based File Input
CSc 110, Autumn 2017 Lecture 21: Line-Based File Input
Building Java Programs
Building Java Programs
Lecture 16: Lists (cont.) and File Input
Building Java Programs
CSc 110, Autumn 2016 Lecture 13: Random Numbers
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2017 Lecture 11: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
while loops; file I/O; introduction to lists
Building Java Programs
CSc 110, Autumn 2016 Lecture 16: File Input.
Building Java Programs
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
CSc 110, Spring 2017 Lecture 13: Random numbers and Boolean Logic
CSc 110, Spring 2017 Lecture 17: Line-Based File Input
Input/output (I/O) import java.io.*;
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
CSc 110, Autumn 2016 Lecture 17: Line-Based File Input
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 21: Line-Based File Input
CSc 110, Autumn 2016 Programming feel like that?
Input/output (I/O) import java.io.*;
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
Building Java Programs
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1 – 6.2, 5.4
Building Java Programs
Presentation transcript:

CSc 110, Autumn 2017 Lecture 18: While loops and File Input Adapted from slides by Marty Stepp and Stuart Reges

Programming Question Write a program that simulates rolling two 6-sided dice until their combined result comes up as 7. 2 + 4 = 6 3 + 5 = 8 5 + 6 = 11 1 + 1 = 2 4 + 3 = 7 You won after 5 tries!

Programming Question Write a program that plays an adding game. Ask user to solve random adding problems with 2-5 numbers. The user gets 1 point for a correct answer, 0 for incorrect. The program stops after 3 incorrect answers. 4 + 10 + 3 + 10 = 27 9 + 2 = 11 8 + 6 + 7 + 9 = 25 Wrong! The answer was 30 5 + 9 = 13 Wrong! The answer was 14 4 + 9 + 9 = 22 3 + 1 + 7 + 2 = 13 4 + 2 + 10 + 9 + 7 = 42 Wrong! The answer was 32 You earned 4 total points

Answer # Asks the user to do adding problems and scores them. from random import * def main(): # play until user gets 3 wrong points = 0 wrong = 0 while wrong < 3: result = play() # play one game if result == 0: wrong += 1 else: points += 1 print("You earned", points, "total points.")

Answer 2 # Builds one addition problem and presents it to the user. # Returns 1 point if you get it right, 0 if wrong. def play(): # print the operands being added, and sum them operands = random.randint(2, 5) sum = random.randint(1, 10) print(sum, end='') for i in range(2, operands + 1): n = random.randint(1, 10) sum += n print(" +", n, end='') print(" = ", end='') # read user's guess and report whether it was correct guess = input() if guess == sum: return 1 else: print("Wrong! The answer was", total) return 0

File Input/output (I/O) name = open("filename") opens the given file for reading, and returns a file object name.read() - file's entire contents as a string >>> f = open("hours.txt") >>> f.read() '123 Brett 12.5 8.1 7.6 3.2\n 456 Sarina 4.0 11.6 6.5 2.7 12\n 789 Nick 8.0 8.0 8.0 8.0 7.5\n'

File paths absolute path: specifies a drive or a top "/" folder C:/Documents/smith/hw6/input/data.csv Windows can also use backslashes to separate folders. relative path: does not specify any top-level folder names.dat input/kinglear.txt Assumed to be relative to the current directory: file = open("data/readme.txt") If our program is in H:/hw6 , open will look for H:/hw6/data/readme.txt

split str.split() - splits a string on blank space You can use the split function to break a file apart str.split() - splits a string on blank space str.split(other_str) - splits a string on occurrences of the other string >>> f = open("hours.txt") >>> text = f.read() '1\n2\n45\n6\n' >>> f = text.split() ['1', '2', '45', '6']

Looping through a file The result of split can be used in a for ... in loop A template for reading files in Python: file = open("filename") text = file.read() text = text.split() for line in text: statements

File input question We have a file weather.txt: 16.2 23.5 19.1 7.4 22.8 18.5 -1.8 14.9 Write a program that prints the change in temperature between each pair of neighboring days. 16.2 to 23.5, change = 7.3 23.5 to 19.1, change = -4.4 19.1 to 7.4, change = -11.7 7.4 to 22.8, change = 15.4 22.8 to 18.5, change = -4.3 18.5 to -1.8, change = -20.3 -1.8 to 14.9, change = 16.7 8 temperatures in the file, but 7 lines of output. It's a fencepost problem in disguise.

File input answer # Displays changes in temperature from data in an input file. def main(): input = open("weather.txt")) lines = input.read().split() prev = float(lines[0]) # fencepost for i in range(1, len(lines)): next = float(lines[i]) print(prev, "to", next, ", change =", (next - prev)) prev = next

Gas prices question Write a program that reads a file gasprices.txt Format: Belgium $/gal US $/gal date 8.20 3.81 3/21/11 8.08 3.84 3/28/11 ... The program should print the average gas price over all data in the file for both countries: Belgium average: 8.3 USA average: 3.9 I don't usually have time to do this program in lecture. It's just here in case I have extra time, or for students to look at later.

Gas prices solution def main(): file = open("gasprices.txt") belgium = 0 usa = 0 count = 0 lines = file.read().split() for i in range(0, len(lines), 3): belgium += float(lines[i]) usa += float(lines[i + 1]) print("Belgium average:", (belgium / count), "$/gal") print("USA average:", (usa / count), "$/gal")