-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_exercise_page200.py
More file actions
134 lines (93 loc) · 3.06 KB
/
python_exercise_page200.py
File metadata and controls
134 lines (93 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# esercizio numero 10.6 pagina 200 (prima parte)
print("Give me two numbers and I'll add them.")
print("Enter 'q' to quit.")
while True:
first_number = input("\nFirst number: ")
if first_number == 'q':
break
second_number = input("Second number: ")
if second_number == 'q':
break
answer = int(first_number) + int(second_number)
print(answer)
# esercizio numero 10.6 seconda parte
print("Give me two numbers and I'll add them.")
print("Enter 'q' to quit.")
while True:
first_number = input("\nFirst number: ")
if first_number.lower() == 'q':
break
second_number = input("Second number: ")
if second_number.lower() == 'q':
break
try:
answer = int(first_number) + int(second_number)
except ValueError:
print("Please enter valid numbers.")
else:
print(f"The answer is {answer}")
# esercizio numero 10.7
print("Give me two numbers and I'll add them.")
print("Enter 'q' to quit.")
while True:
first_number = input("\nFirst number: ")
if first_number == 'q':
break
second_number = input("Second number: ")
if second_number == 'q':
break
answer = int(first_number) + int(second_number)
print(answer)
# esercizio numero 10.6 seconda parte
print("Give me two numbers and I'll add them.")
print("Enter 'q' to quit.")
while True:
first_number = input("\nFirst number: ")
if first_number.lower() == 'q':
break
second_number = input("Second number: ")
if second_number.lower() == 'q':
break
try:
answer = int(first_number) + int(second_number)
except ValueError:
print("Please enter valid numbers.")
else:
print(f"The answer is {answer}")
# esercizio numero 10.8
filenames = ['cats.txt', 'dogs.txt']
for filename in filenames:
print(f"\nReading file: {filename}")
try:
with open(filename) as f:
contents = f.read()
print(contents)
except FileNotFoundError:
print(" Sorry, I can't find that file.")
# esercizio numero 10.9
filenames = ['cats.txt', 'dogs.txt']
for filename in filenames:
try:
with open(filename) as f:
contents = f.read()
except FileNotFoundError:
pass
else:
print(f"\nReading file: {filename}")
print(contents)
# esercizio numero 10.10
def count_common_words(filename, word):
"""Count how many times word appears in the text."""
# Note: This is a really simple approximation, and the number returned
# will be higher than the actual count.
try:
with open(filename, encoding='utf-8') as f:
contents = f.read()
except FileNotFoundError:
pass
else:
word_count = contents.lower().count(word)
msg = f"'{word}' appears in {filename} about {word_count} times."
print(msg)
filename = 'alice.txt'
count_common_words(filename, 'the')