10-1 Python 学习笔记
file_path = 'D:\IDM-download\Python编程-附件\《Python编程》' \
'源代码文件\chapter_10\learning_python.txt'
with open(file_path) as file_object:
print(file_object.read())
with open(file_path) as file_object:
for line in file_object:
print(line.rstrip())
with open(file_path) as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
10-2 C语言学习笔记
file_path = 'D:\IDM-download\Python编程-附件\《Python编程》' \
'源代码文件\chapter_10\learning_python.txt'
with open(file_path) as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip().replace('Python','C'))
10-3 访客
file_path = 'D:\IDM-download\Python编程-附件\《Python编程》' \
'源代码文件\chapter_10\guest.txt'
name = input("Enter your name(Enter 'q' will quit): ")
while True:
if name is 'q':
break
with open(file_path,'a') as file_object:
file_object.write(name)
break
10-4 访客名单
file_path = 'D:\IDM-download\Python编程-附件\《Python编程》' \
'源代码文件\chapter_10\guest_book.txt'
while True:
name = input("Enter your name(Enter 'q' will quit): ")
if name is 'q':
break
name_book = name + ', Hello!'
print(name_book)
with open(file_path,'a') as file_object:
file_object.write(name_book + '\n')
# continue
10-5 关于编程的调查
原理同上
10-6 加法运算
这里并不是TypeError,而是ValueError,具体区别请自行百度
def count_number():
try:
number_1 = input("Enter the first number(Enter 'q' will quit.): ")
if number_1 is 'q':
exit()
number_2 = input("Enter the second number(Enter 'q' will quit.): ")
if number_2 is 'q':
exit()
sum = int(number_1) + int(number_2)
except ValueError:
print("Sorry")
else:
print(sum)
count_number()
10-7 加法计算器
def count_number():
while True:
try:
number_1 = input("Enter the first number(Enter 'q' will quit.): ")
if number_1 is 'q':
exit()
number_2 = input("Enter the second number(Enter 'q' will quit.): ")
if number_2 is 'q':
exit()
sum = int(number_1) + int(number_2)
except ValueError:
print("Sorry")
else:
print(sum)
count_number()
10-8 猫和狗
filenames = ['cats.txt','dogs.txt','cccccc.txt']
def show_file():
try:
with open(filename) as file_object:
print("The file " + filename.title() + " has the following things:")
print(file_object.read())
except FileNotFoundError:
print("Sorry, the file " + filename.title() + " does not exist.")
with open('save_files.txt','a') as a_f:
a_f.write(filename)
for filename in filenames:
show_file()
10-9 沉默的猫和狗
filenames = ['cats.txt','dogs.txt','cccccc.txt']
def show_file():
try:
with open(filename) as file_object:
print("The file " + filename.title() + " has the following things:")
print(file_object.read())
except FileNotFoundError:
# print("Sorry, the file " + filename.title() + " does not exist.")
# with open('save_files.txt','a') as a_f:
# a_f.write(filename)
pass
for filename in filenames:
show_file()
10-10 常见单词
filename = 'alice.txt'
with open(filename) as file_object:
contents = file_object.read()
print(contents.count('the'))
print(contents.lower().count('the'))
10-11 喜欢的数字
import json
filename = 'favorite_number.json'
favorite_number = input("Please enter your favorite number: ")
with open(filename,'w') as f_obj:
json.dump(favorite_number,f_obj)
with open('favorite_number.json') as f:
print(str(json.load(f)))
10-12 记住喜欢的数字
import json
def get_favorite_number():
"""存储了数字,显示出来"""
filename = 'favorite_number.json'
try:
with open(filename) as f:
number = json.load(f)
except FileNotFoundError:
return None
else:
return number
def get_new_number():
"""请用户输入一个数字"""
filename = 'favorite_number.json'
number = input("Enter number: ")
with open(filename,'w') as f:
json.dump(number,f)
# return number
def show_favorite_number():
"""打印数字"""
number = get_favorite_number()
if number:
print(number)
else:
get_new_number()
# print(number)
show_favorite_number()
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():
"""问候用户,并指出其名字"""
filename = 'username.json'
name = input("Please tell me your name: ")
with open(filename) as f:
username = json.load(f)
if name in username:
print("Welcome back, " + name + "!")
# username = get_stored_username()
# if username:
# print("Welcome back, " + username + "!")
else:
name = get_new_username()
print("We'll remember you when you come back, " + name + "!")
greet_user()