第十章习题

本文通过三个实例演示了Python编程的基本操作:记录访客姓名、收集编程爱好者的观点以及进行简单的加法运算。这些例子覆盖了文件读写、循环输入处理及异常处理等知识点。

10-3 访客

filename="program.txt"
with open(filename,'w') as file_object:
	name=input("Please input your name")
	file_object.write(name)

10-5 关于编程的调查

filename="why.txt"
with open(filename,'r+') as file_object:
	while(True):
		reason=input("Why do you like programming? ")
		if(reason):
			file_object.write(reason)
		else:
			break	

10-6 加法运算

print("Give e two numbers,and I will add them")
while(True):
	first=input("First number: ")
	second=input("Second number: ")	
	try:
		answer=int(first)+int(second)
	except ValueError:
		print("You can only print numbers!")
	else:
		print(answer)	
						

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值