10000 Add Chapter 11.5 exercise solutions · MHHamdan/python-basics-exercises@7110008 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7110008

Browse files
committed
Add Chapter 11.5 exercise solutions
1 parent 9dcee98 commit 7110008

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

ch11-file-input-and-output/5-challenge-create-a-high-scores-list-from-csv-data.py

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 11.5 - Reading and Writing Files
2+
# Solutions to Exercises
3+
4+
5+
# Exercise 1
6+
from pathlib import Path
7+
8+
starships = ["Discovery\n", "Enterprise\n", "Defiant\n", "Voyager"]
9+
10+
file_path = Path.home() / "starships.txt"
11+
with file_path.open(mode="w", encoding="utf-8") as file:
12+
file.writelines(starships)
13+
14+
15+
# Exercise 2
16+
with file_path.open(mode="r", encoding="utf-8") as file:
17+
for starship in file.readlines():
18+
print(starship, end="")
19+
20+
21+
# Exercise 3
22+
with file_path.open(mode="r", encoding="utf-8") as file:
23+
for starship in file.readlines():
24+
if starship.startswith("D"):
25+
print(starship, end="")

0 commit comments

Comments
 (0)
0