高级编程技术第十次作业

第十章 文件和异常

10-1 Python学习笔记

with open("hw.txt") as file_object:
    contents = file_object.read()
    print (contents)

with open("hw.txt") as file_object:
    for line in file_object.readlines():
        print (line)

with open("hw.txt") as file_object:
    li = file_object.readlines()
    print (li)

10-2 C语言学习笔记

with open("hw.txt") as file_object:
    contents = file_object.readlines()
    for line in contents:
        line = line.replace("Python", "C")
        print (line.rstrip())

10-3 访客

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

10-4 访客名单

with open("guest_book.txt", "w") as file_object:
    while True:
        name = input("Please input your name: ")
        greeting = "Welcome, " + name
        print (greeting)
        file_object.write(greeting + "\n")

10-5 关于编程的调查

with open("Reason.txt", "w") as file_object:
    while True:
        reason = input("Why do you love programing? ")
        file_object.write(reason + "\n")

10-6 加法运算

print ("Please input two numbers: ")

try:
    number1 = input()
    number1 = int(number1)
    number2 = input()
    number2 = int(number2)
except ValueError:
    print ("Your input is informal")
else:
    print ("The numbers' addition you input is " + str(int(number1) + int(number2)))

10-7 加法计算器

while True:
    print ("Please input two numbers: ")
    try:
        number1 = input()
        number1 = int(number1)
        number2 = input()
        number2 = int(number2)
    except ValueError:
        print ("Your input is informal")
    else:
        print ("The numbers' addition you input is " + str(int(number1) + int(number2)))

10-8 猫和狗

try:
    with open("cats.txt") as file_object:
        print (file_object.read())
except FileNotFoundError:
    print ("The file you want is not exist.")

10-9 沉默的猫和狗

try:
    with open("cats.txt") as file_object:
        print (file_object.read())
except FileNotFoundError:
    pass

10-11 喜欢的数字

import json

filename = "hw.json"
with open(filename, "w") as file_object:
    number = input("Please input your faviourite number: ")
    json.dump(number, file_object)

with open(filename) as file_object:
    number = json.load(file_object)
    print ("I know it " + number)

10-12 记住喜欢的数字

import json

filename = "hw.json"

try:
    with open(filename) as file_object:
        number = json.load(file_object)
        print (number)
except FileNotFoundError:
    with open(filename, "w") as file_object:
        number = input("Please input your faviourite number: ")
        number = int(number)
        json.dump(number, file_object)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值