#10-1 文件读取并输出到屏幕
with open("learning_python.txt" , "r" ) as file:
contents = file.read()
print( contents )
print( "********************")
for ele in contents:
print ( ele )
print( "********************")
with open( "learning_python.txt" , 'r') as file:
row = file.readlines()
print( row )
#10-3 欢迎并保存姓名
name = input("please input your name: ")
print ("Hello , " , name )
with open("guest_book.txt", 'a') as file:
file.write(name )
file.write('\n')
欢迎并保存姓名
name = input("please input your name: ")
print ("Hello , " , name )
with open("guest_book.txt", 'a') as file:
file.write(name )
file.write('\n')
#10-4 欢迎并保存姓名
while True:
name = input("please input your name: ")
print ("Hello , " , name )
with open("guest_book.txt", 'a') as file:
file.write(name )
file.write('\n')
#10-5 爱编程原因
while True:
reason = input("please tell me the reason why you like programming: ")
with open("reason.txt", 'a') as file:
file.write(reason )
file.write('\n')
#10-6 计算和
fir = input( "Please input two integers:")
sec = input()
try:
fir = int(fir)
sec = int(sec)
ans = fir + sec
except ValueError:
print("some of them is not integer")
else :
print('The sum of it is :' , ans )
#10-8 json
import json
def in_number():
number = input("Please input your favorite number: ")
with open('number.json','a' ) as file:
json.dump( number , file)
def out_number():
with open('number.json','r') as file:
number = json.load( file )
print( "I know your favorite number! It's " , number )
in_number()
out_number()