《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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章