教材第10章课后练习
10-1 Python学习笔记
learning_python.txt
In Python you can write hello world.
In Python you can change the type of variables.
In Python you can write less code with the same function.
In Python you can write interesting programs.
main.py
with open('learning_python.txt') as file_object:
print(file_object.read())
print()
with open('learning_python.txt') as file_object:
for line in file_object:
print(line.rstrip())
print()
with open('learning_python.txt') as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
10-2 C语言学习笔记
with open('learning_python.txt') as file_object:
print(file_object.read().replace('Python', 'C'))
10-3 访客
name = input('Please input your name: ')
with open('guest.txt', 'w') as file_object:
file_object.write(name)
10-4 访客名单
with open('guest_book.txt', 'w') as file_object:
while True:
name = input('Please input your name: ')
print('Hello,', name)
file_object.write(name+' has arrived.\n')
10-5 关于编程的调查
while True:
reason = input('Why do you like programming? ')
with open('reasons.txt', 'a') as file_object:
file_object.write(reason+'\n')
10-6 加法运算
try:
num1 = int(input('Please input an integer: '))
num2 = int(input('Please input an integer: '))
print('The sum is', num1+num2)
except ValueError:
print("Sorry, it seems that you did not input an integer.")
10-7 加法计算器
while True:
try:
num1 = int(input('Please input an integer: '))
num2 = int(input('Please input an integer: '))
print('The sum is', num1+num2)
except ValueError:
print("Sorry, it seems that you did not input an integer.")
10-8 猫和狗
try:
with open('cats.txt') as file_object:
cats = file_object.readlines()
for line in cats:
print(line.rstrip())
except FileNotFoundError:
print('cats.txt not found')
try:
with open('dogs.txt') as file_object:
dogs = file_object.readlines()
for line in dogs:
print(line.rstrip())
except FileNotFoundError:
print('dogs.txt not found')
10-9 沉默的猫和狗
try:
with open('cats.txt') as file_object:
cats = file_object.readlines()
for line in cats:
print(line.rstrip())
except FileNotFoundError:
pass
try:
with open('dogs.txt') as file_object:
dogs = file_object.readlines()
for line in dogs:
print(line.rstrip())
except FileNotFoundError:
pass
10-10 常见单词
文件:http://www.gutenberg.org/cache/epub/29444/pg29444.txt
with open('pg29444.txt') as file_object:
print(file_object.read().lower().count('the'))
10-11 喜欢的数字
dump.py
import json
number = input('Please input your favourite number: ')
with open('number.json', 'w') as file_object:
json.dump(number, file_object)
load.py
import json
with open('number.json') as file_object:
number = json.load(file_object)
message = "I know your favourite number! It's xxx.".replace('xxx', str(number))
print(message)
10-12 记住喜欢的数字
import json
try:
with open('number.json') as file_object:
number = json.load(file_object)
message = "I know your favourite number! It's xxx.".replace('xxx', str(number))
print(message)
except FileNotFoundError:
number = input('Please input your favourite number: ')
with open('number.json', 'w') as file_object:
json.dump(number, file_object)
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:
check = input('Are you xxx?(Y/N) '.replace('xxx', username))
if check.upper() == 'Y':
print('Welcome back, ' + username + '!')
return
username = get_new_username()
print("We'll remember you when you come back, "
+ username + "!")
greet_user()