第十章

10-1

with open('learning_python.txt') as file:
    print(file.read())

with open('learning_python.txt') as file:
    for line in file:
        print(line.rstrip())

with open('learning_python.txt') as file:
    leaning = file.readlines()

print(leaning)

10-2

with open('learning_python.txt') as file:
    for line in file:
        print(line.rstrip().replace('Python', 'C++'))

10-3

with open('guest.txt', 'w') as file:
    In = input("Input your name('q' to quit):")
    while In != 'q':
        file.write(In + '\n')
        In = input("Input your name('q' to quit):")

10-7

print("Give me two numbers, and I'll add them.")
print("Enter 'q' to quit.")

while True:
    fnum = input("\nFirst number: ")
    if fnum == 'q':
        break
    snum = input("Second number: ")
    if snum == 'q':
        break
    try:
        print(int(fnum) + int(snum))
    except ValueError:
        print("Invalid number!")
        continue

10-11

import json

lists = []

while True:
    num = input("Enter your favourate number('q' to quit): ")
    if num == 'q':
        break

    lists.append(int(num))

with open('favourate_number.json', 'w') as file:
    json.dump(lists, file)
import json

with open('favourate_number.json', 'r') as file:
    nums = json.load(file)

print("I know your favourite number! It's", end=' ')
for num in nums:
    print(num, end=' ')



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值