python编程:从入门到实践-第十章练习

#10-1
with open ( 'learning_python.txt' ) as lp:

l1 = lp.read()
print (l1)


for i in lp:
print (i)

l2 = lp.readlines()
for i in l2:
print (i.strip())
for i in l2: #10-2
i1 = i.replace( 'Python' , 'Go' )
print (i1)

#10-3
name = input ( 'your name' )
with open ( 'name.txt' , 'a' ) as fn:
fn.write(name)
#10-4
while ( True ):
name = input ( 'your name:' )
print ( 'hello' ,name)
with open ( 'guest_book.txt' , 'a' ) as gn:
gn.write(name)
#10-5
while ( True ):
why = input ( 'why you like coding?' )
with open ( 'because.txt' , 'a' ) as wn:
wn.write(why)


import math
#10-6
a = input ( 'number:' )
b = input ( 'number:' )
try :
a = int (a)
b = int (b)
except ValueError :
print ( 'please give me number!' )
else :
c = a + b

#10-7
while ( True ):
a = input ( 'number:' )
b = input ( 'number:' )
try :
a = int (a)
b = int (b)
except ValueError :
print ( 'please give me number!' )
else :
c = a + b

#10-8
try :
with open ( 'cat.txt' ) as cr:
cat = cr.read()
with open ( 'dog.txt' ) as dr:
dog = dr.read()
except FileNotFoundError :
print ( 'file not found!' )
else :
print (cat)
print (dog)

#10-9
try :
with open ( 'cat.txt' ) as cr:
cat = cr.read()
with open ( 'dog.txt' ) as dr:
dog = dr.read()
except FileNotFoundError :
pass
else :
print (cat)
print (dog)
#10-10
try :
with open ( 'my_book.txt' ) as book:
b = book.read()
except FileNotFoundError :
print ( 'no book!' )
else :
c = b.count( 'the' )
print (c)

#10-11
import json
number = int ( input ( 'your favorite number:' ))
with open ( 'number.txt' , 'a' ) as n:
json.dump(number,n)
with open ( 'number.txt' ) as r:
number = json.load(r)
print (number)
#10-12

try :
with open ( 'number.txt' ) as num:
number = num.read()
except FileNotFoundError :
number = int ( input ( 'your favorite number:' ))
with open ( 'number.txt' , 'a' ) as wr:
json.dump(number,wr)
print (number)
else :
print (number)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值