Python编程 从入门到实践 第十章习题

本文介绍了Python编程学习的第十章习题,包括访客问题的解决方案、加法运算的实现以及探讨了个人喜欢的数字相关编程练习。
部署运行你感兴趣的模型镜像

10-1 Python学习笔记

filename = 'learning_python.txt'
print("读取整个文件:")
with open(filename) as file_object:
    contents = file_object.read()
    print(contents)

print("\n遍历文件对象:")
with open(filename) as file_object:
    for line in file_object:
        print(line.rstrip())

print("\n存储到列表:")
with open(filename) as file_object:
    lines = file_object.readlines()

for line in lines:
    print(line.rstrip())

输出:

读取整个文件:
In Python you can do web programs.
In Python you can do desktop software.
In Python you can develop network programs. 

遍历文件对象:
In Python you can do web programs.
In Python you can do desktop software.
In Python you can develop network programs.

存储到列表:
In Python you can do web programs.
In Python you can do desktop software.
In Python you can develop network programs.

10-3 访客:

filename = 'guest.txt'
name = ""
promt = "Tell me your name, or enter 'quit' to end the program: "
with open(filename, 'w') as file_object:
    while name != 'q':
        name = input(promt)
        if(name == 'q'):
            break
        else:
            print("Hello, " + name.title())
            file_object.write(name + "\n")

输出:

Tell me your name, or enter 'quit' to end the program: Alex
Hello, Alex
Tell me your name, or enter 'quit' to end the program: LEo
Hello, Leo
Tell me your name, or enter 'quit' to end the program: q

10-6 加法运算

def add():
    x = input("Please input the first number: ")
    y = input("Please input the second number: ")
    try:
        num1 = int(x)
        num2 = int(y)
    except ValueError:
        print("Some of your inputs is not a number! ")
    else:
        print(num1 + num2)

add()

输出:

Please input the first number: 34
Please input the second number: df
Some of your inputs is not a number! 
10-8 猫和狗
def pet(filename):
    try:
        with open(filename) as f_obj:
            contents = f_obj.read()
    except FileNotFoundError:
        msg = "\nSorry, the file " + filename + " dose not exist."
        print(msg)
    else:
        print("\n" + contents)


filenames = ["cat.txt", "bird.txt", "dog.txt"]
for filename in filenames:
    pet(filename)

输出:

Lili cat
NiNi cat
dongdong cat

Sorry, the file bird.txt dose not exist.

Alex dog
LEo dog
Mike dog

10-11 喜欢的数字:

import json

filename = 'number.json'
try:
    with open(filename) as f_obj:
        number = json.load(f_obj)
except FileNotFoundError:
    number = input("What is your favorite number ?")
    with open(filename, 'w') as f_obj:
        json.dump(number, f_obj)
        print("Welcome, we will remember your favorite number " + number)
else:
    print("Welcome back, your favorite number is " + number )

def get_number():
    with open(filename) as f_obj:
        print("I know your favorite number! It's " + json.load(f_obj))

get_number()

输出:

Welcome back, your favorite number is 12
I know your favorite number! It's 12

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值