python编程从入门到实践第十章习题答案

#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()

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值