Skip to content

Commit eb0f60c

Browse files
committed
完成第十章文件和异常练习
1 parent 997fcde commit eb0f60c

File tree

17 files changed

+194
-0
lines changed

17 files changed

+194
-0
lines changed

chapter10/10-1.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# encoding: utf-8
2+
3+
filename = 'learning_python.txt'
4+
5+
# 第一次打印读取整个文件
6+
print("--- Reading in the entire file:")
7+
with open(filename) as f:
8+
contents = f.read()
9+
print(contents)
10+
11+
# 第二次打印时遍历文件对象
12+
print("\n--- Looping over the lines:")
13+
with open(filename) as f:
14+
for line in f:
15+
print(line.rstrip())
16+
17+
# 第三次打印时将各行存储在一个列表中,再在with代码块外打印它们
18+
print("\n--- Storing the lines in a list:")
19+
with open(filename) as f:
20+
lines = f.readlines()
21+
22+
for line in lines:
23+
print(line.rstrip())

chapter10/10-11-read.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import json
2+
3+
with open('favorite_number.json') as f:
4+
number = json.load(f)
5+
6+
print("I know your favorite number! It's " + str(number) + ".")

chapter10/10-11-write.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import json
2+
3+
number = input("What's your favorite number? ")
4+
5+
with open('favorite_number.json', 'w') as f:
6+
json.dump(number, f)
7+
print("Thanks! I'll remember that.")

chapter10/10-12.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import json
2+
3+
try:
4+
with open('favorite_number.json') as f:
5+
number = json.load(f)
6+
except FileNotFoundError:
7+
number = input("What's your favorite number? ")
8+
with open('favorite_number.json', 'w') as f:
9+
json.dump(number, f)
10+
print("Thanks, I'll remember that.")
11+
else:
12+
print("I know your favorite number! It's " + str(number) + ".")

chapter10/10-13.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
3+
def get_stored_username():
4+
"""Get stored username if available."""
5+
filename = 'username.json'
6+
try:
7+
with open(filename) as f_obj:
8+
username = json.load(f_obj)
9+
except FileNotFoundError:
10+
return None
11+
else:
12+
return username
13+
14+
def get_new_username():
15+
"""Prompt for a new username."""
16+
username = input("What is your name? ")
17+
filename = 'username.json'
18+
with open(filename, 'w') as f_obj:
19+
json.dump(username, f_obj)
20+
return username
21+
22+
def greet_user():
23+
"""Greet the user by name."""
24+
username = get_stored_username()
25+
if username:
26+
correct = input("Are you " + username + "? (y/n) ")
27+
if correct == 'y':
28+
print("Welcome back, " + username + "!")
29+
else:
30+
username = get_new_username()
31+
print("We'll remember you when you come back, " + username + "!")
32+
else:
33+
username = get_new_username()
34+
print("We'll remember you when you come back, " + username + "!")
35+
36+
greet_user()

chapter10/10-2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# encoding: utf-8
2+
3+
filename = './learning_python.txt'
4+
5+
with open(filename) as f:
6+
lines = f.readlines()
7+
8+
for line in lines:
9+
line = line.rstrip()
10+
print(line.replace('Python', 'C'))

chapter10/10-3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name = input("What's your name? ")
2+
3+
filename = 'guest.txt'
4+
5+
with open(filename, 'w') as f:
6+
f.write(name)

chapter10/10-4.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
filename = 'guest_book.txt'
2+
3+
print("Enter 'quit' when you are finished.")
4+
while True:
5+
name = input("\nWhat's your name? ")
6+
if name == 'quit':
7+
break
8+
else:
9+
with open(filename, 'a') as f:
10+
f.write(name + "\n")
11+
print("Hi " + name + ", you've been added to the guest book.")

chapter10/10-5.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
filename = 'programming_poll.txt'
2+
3+
responses = []
4+
while True:
5+
response = input("\nWhy do you like programming? ")
6+
responses.append(response)
7+
8+
continue_poll = input("Would you like to let someone else respond? (y/n) ")
9+
if continue_poll != 'y':
10+
break
11+
12+
with open(filename, 'a') as f:
13+
for response in responses:
14+
f.write(response + "\n")

chapter10/10-6.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
try:
2+
x = input("Give me a number: ")
3+
x = int(x)
4+
5+
y = input("Give me another number: ")
6+
y = int(y)
7+
8+
except ValueError:
9+
print("Sorry, I really needed a number.")
10+
11+
else:
12+
sum = x + y
13+
print("The sum of " + str(x) + " and " + str(y) + " is " + str(sum) + ".")

0 commit comments

Comments
 (0)