Skip to content

Commit bd3ea63

Browse files
committed
update Hamlet exercise
1 parent f71ebec commit bd3ea63

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

07-files/files5.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# Define file path with an "r"-string
55
filepath = r'07-files/soliloquy.txt'
66

7-
new_lines = ["To sleep, perchance to Dream; aye, there's the rub,",
8-
"For in that sleep of death, what dreams may come,",
7+
new_lines = ["To sleep, perchance to dream; ay, there's the rub;",
8+
"For in that sleep of death what dreams may come",
99
"When we have shuffled off this mortal coil,",
1010
"Must give us pause."]
1111

@@ -18,19 +18,23 @@
1818

1919
# The line count will be the position given by `enumerate()`, plus 1
2020
num_lines = position + 1
21-
print(f"File is {num_lines} lines long.")
21+
print(f"\nFile is {num_lines} lines long.\n")
22+
23+
print("*" * 10)
24+
print("Hamlet, Act III, scene I")
25+
print("*" * 10 + '\n')
2226

2327
# Open in append mode ('a')
2428
with open(filepath, mode='a') as writer:
2529

26-
# Write each line in the list to the file, and add a newline character;
30+
# Append each line in the list to the file, and add a newline character;
2731
# without it, all the new lines would have been concatenated.
2832
for line in new_lines:
2933
writer.write(line + '\n')
3034

3135
# Print the newly added-to file, with line numbers.
3236
with open(filepath, mode='r') as reader:
3337
for line_num, line in enumerate(reader, start=1):
34-
print(line_num, line)
38+
print(line_num, line, end='')
3539

36-
print(f"Wrote {len(new_lines)} extra lines.")
40+
print(f"\nWrote {len(new_lines)} extra lines.")

07-files/soliloquy.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
To be, or not to be, that is the question,
1+
To be, or not to be, that is the question:
22
Whether 'tis nobler in the mind to suffer
33
The slings and arrows of outrageous fortune,
44
Or to take arms against a sea of troubles,
55
And by opposing end them? To die: to sleep;
66
No more; and by a sleep, to say we end
77
The heart-ache, and the thousand natural shocks
8-
That Flesh is heir to? 'Tis a consummation
9-
Devoutly to be wished. To die, to sleep,
8+
That Flesh is heir to, 'tis a consummation
9+
Devoutly to be wish'd. To die, to sleep,

0 commit comments

Comments
 (0)