Python编程作业【第六周】(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaobinqi_98/article/details/79946670

10-9 silent cat and dog

try:
    with open("cats.txt") as obj:
        for line in obj.readlines(): print(line, end = '')
    with open("dogs.txt") as obj:
        for line in obj.readlines(): print(line)
except FileNotFoundError:
    pass

10-11 favorite number

import json
fav_number = input("Please input your favorite number: ")
filename = "favo_number.json" # must .json
with open(filename, 'w') as obj:
    json.dump(fav_number, obj)
import json
filename = "favo_number.json" # must .json
with open(filename, 'r') as obj:
    number = json.load(obj)
print("I know your favourite number! It is " + str(number))

10-12 remember favorite number

import json
filename = "favo_number.json" # must .json
try:
    with open(filename, 'r') as obj:
        number = json.load(obj)
except FileNotFoundError:
    number = input("Please input your favorite number: ")
    with open(filename, 'w') as obj:
        json.dump(number, obj)
print("I know your favourite number! It is " + str(number))
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页