Python程式設計:從入門到實踐的動手試一試答案(第十章)
阿新 • • 發佈:2019-02-02
#10-1 Python學習筆記
with open('learning_python.txt') as file_object:
contents = file_object.read()
print(contents)
----------------------------------------------
with open('learning_python.txt') as file_object:
for line in file_object:
print(line.rstrip())
----------------------------------------------
with open('learning_python.txt') as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
#10-2 C語言學習筆記
with open('learning_python.txt') as file_object:
lines = file_object.readlines()
for line in lines:
line1 = line.replace('Python' ,'C')
print(line1.rstrip())
- 1
- 2
- 3
- 4
- 5
- 6
- 7
#10-3 訪客
filename = 'guest.txt'
with open(filename,'w') as file_object:
name = input('請輸入你的名字:')
file_object.write(name)
- 1
- 2
- 3
- 4
- 5
#10-4 訪客名單
filename = 'guest_book.txt'
with open(filename,'a') as file_object:
print('按‘q’推出')
while True:
name = input('請輸入你的名字:' )
if name == 'q':
break
file_object.write(name + '\n')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
#10-5 關於程式設計的調查
filename = 'reason.txt'
with open(filename,'a') as file_object:
print('按‘q’推出')
while True:
reason = input('請問你為何喜歡程式設計:')
if reason == 'q':
break
file_object.write(reason + '\n')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
#10-6 加法運算
try:
number1 = int(input('請輸入一個數字:'))
number2 = int(input('請在輸入一個數字:'))
result = number1 + number2
print(result)
except ValueError:
print('你輸入的格式有問題')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
#10-7 加法計算器
while True:
try:
number1 = int(input('請輸入一個數字:'))
number2 = int(input('請在輸入一個數字:'))
result = number1 + number2
print(result)
except ValueError:
print('你輸入的格式有問題')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
#10-8 貓和狗
try:
with open('cats.txt') as cat:
print(cat.read())
with open('dogs.txt') as dog:
print(dog.read())
except FileNotFoundError:
print('對不起沒有找到檔案')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
#10-9 沉默的貓和狗
try:
with open('cats.txt') as cat:
print(cat.read())
with open('dogs.txt') as dog:
print(dog.read())
except FileNotFoundError:
pass
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
#10-10 常見單詞
try:
with open("The Girl Scouts' Canoe Trip by Edith Lavell.txt") as book:
content = book.read()
number = content.lower().count('the')
print(number)
except FileNotFoundError:
pass
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
#10-11 喜歡的數字
import json
filename = 'numbers.json'
number = input('請輸入你最喜歡的數字:')
with open(filename, 'w') as f_obj:
json.dump(number, f_obj)
with open(filename) as f_obj:
number = json.load(f_obj)
print('I know your favorite number! It is ___' + number)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
#10-12 記住喜歡的數字
import json
filename = 'numbers.json'
try:
with open(filename) as f_obj:
number = json.load(f_obj)
print('I know your favorite number! It is ___' + number)
except FileNotFoundError:
number = input('請輸入你最喜歡的數字:')
with open(filename, 'w') as f_obj:
json.dump(number, f_obj)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
#10-13 驗證使用者
import json
def get_stored_username():
"""如果儲存了使用者名稱,就獲取它"""
filename = 'username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
return None
else:
return username
def get_new_username():
"""提示使用者輸入使用者名稱"""
username = input("What is your name? ")
filename = 'username.json'
with open(filename, 'w') as f_obj:
json.dump(username, f_obj)
return username
def greet_user():
"""問候使用者,並指出其名字"""
username = get_stored_username()
if username:
yep = input('Is ' + username + ' your name?(y/n)')
if yep == 'y':
print("Welcome back, " + username + "!")
else:
username = get_new_username()
print("We'll remember you when you come back, " + username + "!")
else:
username = get_new_username()
print("We'll remember you when you come back, " + username + "!")
greet_user()