《Python编程 从入门到实践》第十章习题选做

10-1 Python学习笔记

在learning_python.txt文件:

In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.
filename = 'learning_python.txt'
with open(filename) as f:
    s = f.read()
    print(s)
print("**********************")
with open(filename) as f:
    for line in f:
        print(line.rstrip())
print("**********************")
with open(filename) as f:
    lines = f.readlines()
for line in lines:
    print(line.rstrip())

输出:

In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.
**********************
In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.
**********************
In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.

10-2 C语言学习笔记

filename = 'learning_python.txt'
with open(filename) as f:
    lines = f.readlines()
for line in lines:
    print(line.replace('Python', 'C').rstrip())

输出:

In C you can learn how to program.
In C you can feel every interested.
In C you can learn how to solve hard problem.

10-3 访客名单

filename = 'guest_book.txt'
with open(filename, 'w') as f_obj:
    while True:
        print("input 'quit' to exit.")
        username = input("Please inout your name: ")
        if username == 'quit':
            break
        print("Hello " + username + ".")
        f_obj.write(username + " has already visited.\n")

输出:

input 'quit' to exit.
Please inout your name: Li Hua
Hello Li Hua.
input 'quit' to exit.
Please inout your name: John
Hello John.
input 'quit' to exit.
Please inout your name: Amy
Hello Amy.
input 'quit' to exit.
Please inout your name: quit

文件guest_book.txt:

Li Hua has already visited.
John has already visited.
Amy has already visited.

10-7 加法计算器

print("Give me two numbers, and I'll add them.")
print("Enter'q' to quit.")
msg = "Please input two numbers, and separated by space.\n"

while True:
    try:
        str = input(msg)
        if str == 'q':
            break
        first, second = map(int, str.split())
    except ValueError:
        print("Please enter two pure numbers.")
    else:
        print(first + second)

输出:

Give me two numbers, and I'll add them.
Enter'q' to quit.
Please input two numbers, and separated by space.
we 34
Please enter two pure numbers.
Please input two numbers, and separated by space.
23 rt
Please enter two pure numbers.
Please input two numbers, and separated by space.
sd er
Please enter two pure numbers.
Please input two numbers, and separated by space.
34 78
112
Please input two numbers, and separated by space.
q

10-8 猫和狗

def print_animals_name(filename):
    try:
        with open(filename) as f_obj:
            lines = f_obj.readlines()
    except FileNotFoundError:
        msg = "Sorry, the file " + filename + " doesn't exist."
        print(msg)
    else:
        for line in lines:
            print(line.rstrip())


filenames = ['cats.txt', 'dogs.txt']
for filename in filenames:
    print(filename + ": ")
    print_animals_name(filename)

两个文件都存在时的输出:

cats.txt:
Groot
Cesar
Wendy
Baccano
dogs.txt:
Pitt
Dick
Mike
Nick
Alice

cats.txt不存在时的输出:

cats.txt:
Sorry, the file cats.txt doesn't exist.
dogs.txt:
Pitt
Dick
Mike
Nick
Alice

10-9 沉默的猫和狗

def print_animals_name(filename):
    try:
        with open(filename) as f_obj:
            lines = f_obj.readlines()
    except FileNotFoundError:
        pass
        # msg = "Sorry, the file " + filename + " doesn't exist."
        # print(msg)
    else:
        for line in lines:
            print(line.rstrip())


filenames = ['cats.txt', 'dogs.txt']
for filename in filenames:
    print(filename + ": ")
    print_animals_name(filename)

cats.txt不存在:

cats.txt:
dogs.txt:
Pitt
Dick
Mike
Nick
Alice

10-10 常见单词

filename = 'Pennsylvania Journal.txt'
with open(filename) as f_obj:
    contents = f_obj.read()
    num = contents.lower().count('the')
    print(num)

输出:

2668

10-11 喜欢的数字

import json
filename = 'favorite_number.json'
with open(filename, 'w') as f_obj:
    number = input("Please input your favorite number; ")
    json.dump(number, f_obj)


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

输出:

Please input your favorite number; 34
I know your favorite number! It's 34

10-12 记住喜欢的数字

import json
filename = 'favorite_number.json'
test = 2
while test != 0:
    test -= 1
    try:
        with open(filename) as f_obj:
            number = json.load(f_obj)
    except FileNotFoundError:
        with open(filename, 'w') as f_obj:
            number = input("Please input your favorite number; ")
            json.dump(number, f_obj)
    else:
        msg = "I know your favorite number!"
        print(msg + " It's " + str(number))

输出:

Please input your favorite number; 56
I know your favorite number! It's 56
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值