您的位置:首页 > 编程语言 > Python开发

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

2018-04-08 20:50 681 查看
#10-1with 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-3name = input('your name')with open('name.txt','a') as fn: fn.write(name)#10-4while(True): name = input('your name:') print('hello',name) with open('guest_book.txt','a') as gn: gn.write(name)#10-5while(True): why = input('why you like coding?') with open('because.txt','a') as wn: wn.write(why)

import math#10-6a = input('number:')b = input('number:')try: a=int(a) b=int(b)except ValueError: print('please give me number!')else: c=a+b
#10-7while(True): a = input('number:') b = input('number:') try: a=int(a) b=int(b) except
4000
ValueError: print('please give me number!') else: c=a+b
#10-8try: 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-9try: with open('cat.txt') as cr: cat = cr.read() with open('dog.txt') as dr: dog = dr.read()except FileNotFoundError: passelse: print(cat) print(dog)#10-10try: with open('my_book.txt') as book: b = book.read()except FileNotFoundError: print('no book!')else: c = b.count('the') print(c)
#10-11import jsonnumber = 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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python