Skip to content

Commit 8536bbf

Browse files
committed
csv lessons
1 parent 617c910 commit 8536bbf

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

days/13-15-text-games/rps_classes.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,24 @@ def print_header():
5151
print(" 15-way Rock Paper Scissors")
5252
print("=" * 30)
5353

54+
5455
def best_of(player1):
5556
print(f"Welcome {player1.name}!\n")
5657
while True:
5758
try:
5859
best_of_num = int(input("Enter an odd number of rounds to play:"))
5960
if best_of_num % 2 == 0:
6061
raise EvenNumberException
61-
print(f"OK {player1.name}, let's play best of {best_of_num} rounds. Good Luck!\n")
62+
print(
63+
f"OK {player1.name}, let's play best of {best_of_num} rounds. Good Luck!\n"
64+
)
6265
break
6366
except ValueError:
6467
print("You did not enter an number.\n")
6568
except EvenNumberException:
66-
print("You entered an even number which means the match could end in a tie.\n")
67-
69+
print(
70+
"You entered an even number which means the match could end in a tie.\n"
71+
)
6872
return best_of_num
6973

7074

@@ -88,6 +92,7 @@ def game_loop(player1, player2, BEST_OF_NUM):
8892
time.sleep(0.25)
8993
p2_turn = get_computers_selection()
9094
print(f"The Computer chose {p2_turn.action}")
95+
9196
determine_winner(p1_turn, p2_turn, player1, player2, victories)
9297
get_score(player1, player2)
9398
input("Press ENTER to move to the next round.")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import myresearch
2+
3+
4+
def main():
5+
myresearch.init()
6+
print("Weather research for Seattle, 2014-2015")
7+
print()
8+
9+
print("The hottest 5 days:")
10+
days = myresearch.hot_days()
11+
for i, day in enumerate(days[:5]):
12+
print(f"{i+1}. {day.actual_max_temp} F on {day.date}")
13+
print()
14+
15+
print("The coldest 5 days:")
16+
days = myresearch.cold_days()
17+
for i, day in enumerate(days[:5]):
18+
print(f"{i+1}. {day.actual_min_temp} F on {day.date}")
19+
print()
20+
21+
print("The wettest 5 days:")
22+
days = myresearch.wet_days()
23+
for i, day in enumerate(days[:5]):
24+
print(f"{i+1}. {day.actual_precipitation}\" on {day.date}")
25+
print()
26+
27+
28+
29+
if __name__ == "__main__":
30+
main()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import csv
2+
import os
3+
from collections import namedtuple
4+
from typing import List
5+
6+
data = []
7+
8+
Record = namedtuple(
9+
"Record",
10+
"date,actual_mean_temp,actual_min_temp,actual_max_temp,average_min_temp,"
11+
"average_max_temp,record_min_temp,record_max_temp,record_min_temp_year,"
12+
"record_max_temp_year,actual_precipitation,average_precipitation,record_precipitation"
13+
)
14+
15+
16+
def init():
17+
base_folder = os.path.dirname(__file__)
18+
filename = os.path.join(base_folder, "data", "seattle.csv")
19+
20+
with open(filename, "r", encoding="utf-8") as f:
21+
reader = csv.DictReader(f)
22+
23+
data.clear()
24+
for row in reader:
25+
record = parse_row(row)
26+
data.append(record)
27+
28+
29+
def parse_row(row):
30+
row["actual_mean_temp"] = int(row["actual_mean_temp"])
31+
row["actual_min_temp"] = int(row["actual_min_temp"])
32+
row["actual_max_temp"] = int(row["actual_max_temp"])
33+
row["average_min_temp"] = int(row["average_min_temp"])
34+
row["average_max_temp"] = int(row["average_max_temp"])
35+
row["record_min_temp"] = int(row["record_min_temp"])
36+
row["record_max_temp"] = int(row["record_max_temp"])
37+
row["record_min_temp_year"] = int(row["record_min_temp_year"])
38+
row["record_max_temp_year"] = int(row["record_max_temp_year"])
39+
row["actual_precipitation"] = float(row["actual_precipitation"])
40+
row["average_precipitation"] = float(row["average_precipitation"])
41+
row["record_precipitation"] = float(row["record_precipitation"])
42+
43+
record = Record(**row)
44+
45+
return record
46+
47+
def hot_days() -> List[Record]:
48+
return sorted(data, key=lambda r: -r.actual_max_temp)
49+
50+
def cold_days() -> List[Record]:
51+
return sorted(data, key=lambda r: r.actual_max_temp)
52+
53+
def wet_days() -> List[Record]:
54+
return sorted(data, key=lambda r: -r.actual_precipitation)
55+
56+

0 commit comments

Comments
 (0)