10-2 C语言学习笔记
file_name = "learning_python.txt"
with open(file_name) as file_object:
lines = file_object.readlines()
for line in lines:
print(line.replace("Python", "C"))
learning_python.txt
In Python you can store as much information as you want.
In Python you can connect pieces of information.
In Python you can model real-world situations.
10-4 访客名单
file_name = "guest_book.txt"
while 1:
print("Please enter your name, enter 'exit' to exit")
name = input()
if name == 'exit':
break
else:
print(name.title() + ", welcome!")
with open(file_name, 'a') as f:
f.write(name.title() + " has visited.\n")
guest_book.txt
Tom has visited.
Jim has visited.
Kessef has visited.
10-6 加法运算
try:
x = input("Please enter a number:")
x = int(x)
y = input("Please enter another number:")
y = int(y)
except ValueError:
print("Sorry it is not a number")
else:
sum = x + y
print("The sum is: " + str(sum))
10-11 喜欢的数字
favorite_number_write.py
import json
file_name = "favorite_number.json"
number = input("Please input your favorite number: ")
with open(file_name, 'w') as f:
json.dump(number, f)
favorite_number_read.py
import json
file_name = "favorite_number.json"
with open(file_name) as f:
number = json.load(f)
print("I know your favorite number! It's " + str(number))