withopen("learning_python.txt") as file_object:
print(file_object.read())
print()
withopen("learning_python.txt") as file_object:
forlinein file_object:
print(line.rstrip())
print()
withopen("learning_python.txt") as file_object:
lines = file_object.readlines()
forlineinlines:
print(line.rstrip())
10-2 C语言学习笔记
withopen("learning_python.txt") as file_object:
lines = file_object.readlines()
forlineinlines:
line.replace('Python', 'C')
print(line.rstrip())
10-3 访客
name = input("Please enter your name:\t")
withopen("guest.txt", 'w') as file_object:
file_object.write(name)
10-4 访客名单
name = ""while True:
name = input("Please enter your name:\t")
ifname == 'quit':
break;
else:
print("Hello, " + name + ".")
with open("guest_book.txt", 'a') as file_object:
file_object.write(name + "\n")
10-5 关于编程的调查
whileTrue:
reason = input("Please enter why you love programing:\t")
if reason == 'quit':
breakelse:
with open("programing_reason.txt", 'a') as file_obj:
file_obj.write(reason + '\n')
10-6 加法运算
try:
num1 = int(input("Please enter the first integer:\t"))
num2 = int(input("Please enter the second integer:\t"))
except :
print("Sorry, please enter an integer")
else:
print(str(num1 + num2))
10-7 加法计算器
whileTrue:
try:
num1 = input("Please enter the first integer('quit' to quit):\t")
if num1 == 'quit':
break
num2 = input("Please enter the second integer('quit' to quit):\t")
if num2 == 'quit':
break
num1 = int(num1)
num2 = int(num2)
except ValueError:
print("Sorry, please enter an integer")
else:
print(str(num1 + num2))
10-8 猫和狗
try:
with open("cats.txt") as cats:
for line in cats:
print(line.rstrip())
except FileNotFoundError:
print("File not found!(cats.txt)")
try:
with open("dogs.txt") as dogs:
for line in dogs:
print(line.rstrip())
except FileNotFoundError:
print("File not found!(dogs.txt)")
10-9 沉默的猫和狗
try:
with open("cats.txt") as cats:
for line in cats:
print(line.rstrip())
except FileNotFoundError:
passtry:
with open("dogs.txt") as dogs:
for line in dogs:
print(line.rstrip())
except FileNotFoundError:
pass
10-10 常见单词
try:
with open("book.txt") as book:
bookStr = book.read()
print(str(bookStr.lower().count('the')))
except FileNotFoundError:
pass
10-11 喜欢的数字
# favorite_num_dump.pyimport json
file_name = "favorite_num.json"try:
num = int(input("Please enter your favorite integer:\t"))
except ValueError:
print("Error! Not a integer!")
else:
with open(file_name, 'w') as file_obj:
json.dump(num, file_obj)
# favorite_num_load.pyimport json
file_name = "favorite_num.json"try:
with open(file_name) as file_obj:
num = json.load(file_obj)
print("I know your favorite number! It's " + str(num) + ".")
except FileNotFoundError:
print("File not found!(favorite_num.json)")
10-12 记住喜欢的数字
import json
file_name = "favorite_num.json"try:
withopen(file_name) as file_obj:
num = json.load(file_obj)
except FileNotFoundError:
try:
num = int(input("Please enter your favorite integer:\t"))
except ValueError:
print("Error! Not a integer!")
else:
withopen(file_name, 'w') as file_obj:
json.dump(num, file_obj)
else:
print("I know your favorite number! It's " + str(num) + ".")
10-13 验证用户
import json
defget_stored_username():
filename = 'username.json'try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
returnNoneelse:
return username
defget_new_username():
username = input("What is you name?\t")
filename = 'username.json'with open(filename, 'w') as f_obj:
json.dump(username, f_obj)
return username
defgreet_user():
username = get_stored_username()
if username:
print("Username:\t" + username)
message = "Is this user name right?(Yes/No)\t"
ok = input(message)
if ok == 'Yes':
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()