第五周作业_Chapter 10课后练习

10-1 Python学习笔记

代码:

with open('learning_python.txt') as fo:
content = fo.read()
print(content)

print("------------------------------------------------")
with open('learning_python.txt') as fo:
for line in fo:
print(line.rstrip())

print("------------------------------------------------")
with open('learning_python.txt') as fo:
lines = fo.readlines()
for line in lines:
print(line.rstrip())

运行结果:



10-4 访客名单

代码:

with open('guest_book.txt','w') as fo:
while(True):
name = input("Please enter your name: ")
if name=='q':
break
message = "Hello, " + name + "!\n"
print(message)
fo.write(message)

运行结果:




10-7 加法计算器

代码:

while True:
a = input("You are plusing two numbers.Please enter the first number: ")
b = input("Please enter the other number: ")
try:
a = int(a)
b = int(b)
except ValueError:
print("Your enter is invalid, please enter again.")
continue
else:
c = a+b
print("The sum is " + str(c) + ".")
break

运行结果:



10-12 记住喜欢的数字

代码:

运行结果:

import json
try:
with open('number.json') as f_obj:
content = f_obj.read()
except FileNotFoundError:
with open('number.json','w') as f_obj:
number = input("Please enter your favorite number: ")
message = "I know your favorite number, it's " + number + "!"
json.dump(message,f_obj)
else:
print(content)

第一次:


第二次:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值