for Looppython3Are there any questions before I begin?
I have posted a solution to homework 7 here.
Let's take a look.
I have posted homework 9 here.
It is due this coming Sunday at 11:59 PM.
fahrenheit = 9/5 * celsius + 32
def celsius_to_fahrenheit(celsius):
return round(9/5 * celsius + 32)
for Loopwhile loop to read a filefor loop is the best way to work with a file ...for loop
for loop
$ cat 1-5.txt
1
2
3
4
5
>>> file = open('1-5.txt', 'r')
>>> for line in file:
... line = file.readline()
... print(line.strip())
...
2
4
for loop reads the next lineline = line.strip()
int or
float
str conversion function
>>> file = open("numbers.txt","w")
>>> file.write(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not int
for Loopfor loopfor loop has this format
for LOOP_VARIABLE in LIST_OF_SOME_KIND:
for loop
for line in file:
print(line.strip())
python3
python3 at the command line ...$ python3 Python 3.5.2 (default, Apr 16 2020, 17:47:17) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
python3 followed by the name of the script
# prints a friendly message
print("Hello world!")
$ python3 hello_1.py Hello world!
# prints a friendly message echo Hello world
bash command
$ bash hello_1.sh Hello world!
bash can run a Bash scriptpython3 runs a Python scriptchmod ...$ chmod 755 hello_1.sh
bash
$ ./hello_1.sh Hello world!
$ chmod 755 hello_1.py
$ ./hello_1.py
./hello_1.py: line 3: syntax error near unexpected token `"Hello world!"
./hello_1.py: line 3: `print("Hello world!")'
python3
on the file ...
bash#!
bashchmod
chmod 755 FILENAME
#! /usr/bin/python3
#! /usr/bin/python3
# prints a friendly message
print("Hello world!")
python3
$ ./hello_2.py Hello world!
#! /usr/bin/perl
python3 before the name of a file is no great hardshippython3 email_missing.py
python3 to run the script I would have to write
python3 /home/ghoffman/bin/python/umb/email_missing.py
email_missing.py
email_missing.py on the command line ...for loop to average the numbers in a file$ cat numbs.txt 1 2 3 4 5 6 7 8 9 10
filename = input("Filename: ")
file = open(filename, "r")
total = 0 count = 0
for loop that will read each linecount += 1
total += int(line)
average = round(total/count, 2)
print("Average:", average)
#! /usr/bin/python3
# reads numbers from a file and averages them
filename = input("Filename: ")
file = open(filename, "r")
total = 0
count = 0
for line in file:
count += 1
total += int(line)
average = round(total/count, 2)
print("Average:", average)
$ ./for_average.py
Filename: numbs.txt
Average: 5.5
$ cat temps.txt 2017-06-01 67 2017-06-02 71 2017-06-03 69 2017-06-04 88 2017-06-05 74 ...
split
split turns a string into a list of valuessplit ...>>> line = '2017-06-01 67' >>> line.split() ['2017-06-01', '67']
>>> date, temp = line.split()
$ cat temps_print.py
#! /usr/bin/python3
# prints each field in temps.txt
file = open('temps.txt', 'r')
for line in file:
date, temp = line.split()
print(date, temp)
$ ./temps_print.py
2017-06-01 67
2017-06-02 71
2017-06-03 69
2017-06-04 88
2017-06-05 74
...
2017-06-01,67
split ...
>>> line = '2017-06-01,67'
>>> line.split(",")
['2017-06-01', '67']
count = 0 total = 0
for loop header stays the samecount += 1
temp = int(temp) total += temp
average = round(total/count, 2)
print('Average:', average)
$ cat temps_average.py
#! /usr/bin/python3
# calculates the average the temperature in temps.txt
file = open('temps.txt', 'r')
count = 0
total = 0
for line in file:
count += 1
date, temp = line.split()
temp = int(temp)
total += temp
average = round(total/count, 2)
print('Average:', average)
$ ./temps_average.py Average: 77.27
set max to the lowest possible temperature
for each temperature in the file
if the temperature is greater than max
set the max to this temperature
max = -100
if statement to the loop
if temp > max:
max = temp
set min to the highest possible temperature
for each temperature in the file
if the temperature is less than min
set min to this temperature
min = 200
if temp < min:
min = temp
$ cat temps_max_min_average.py
#! /usr/bin/python3
# calculates the average, maximum and minimum in temps.txt
file = open('temps.txt', 'r')
count = 0
total = 0
max = -100
min = 200
for line in file:
count += 1
date, temp = line.split()
temp = int(temp)
total += temp
if temp > max:
max = temp
if temp < min:
min = temp
average = round(total/count, 2)
print('Average:', average)
print('Maximum:', max)
print('Minimum:', min)
$ ./temps_max_min_average.py Average: 77.27 Maximum: 89 Minimum: 66
file.close()
close() frees up the memory use by a object
set days_above to 0
for each temperature in the file
if the temperature is greater than the average
add 1 to the days_above
file = open('temps.txt', 'r')
days_above = 0
for line in file:
date, temp = line.split()
temp = int(temp)
if temp > average:
days_above += 1
print("Days above average:", days_above)
$ ./temps_days_above_average.py Days above average: 13