1. 程式人生 > >第十章課後作業

第十章課後作業

Chapter 10

10-1 Python學習筆記

with open("learning_python.txt") as file_object:
    print(file_object.read())
print()
with open("learning_python.txt") as file_object:
    for line in file_object:
        print(line.rstrip())
print()
with open("learning_python.txt") as file_object:
    lines = file_object.readlines()
    for
line in lines: print(line.rstrip())

10-2 C語言學習筆記

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

10-3 訪客

name = input("Please enter your name:\t")
with open("guest.txt"
, 'w') as file_object: file_object.write(name)

10-4 訪客名單

name = ""
while True:
    name = input("Please enter your name:\t")
    if name == 'quit':
        break;
    else:
        print("Hello, " + name + ".")
        with open("guest_book.txt", 'a') as file_object:
            file_object.write
(name + "\n")

10-5 關於程式設計的調查

while True:
    reason = input("Please enter why you love programing:\t")
    if reason == 'quit':
        break
    else:
        with open("programing_reason.txt", 'a') as file_obj:
            file_obj.write(reason + '\n')

10-6 加法運算

try:
    num1 = int(input("Please enter the first integer:\t"))
    num2 = int(input("Please enter the second integer:\t"))
except :
    print("Sorry, please enter an integer")
else:
    print(str(num1 + num2))

10-7 加法計算器

while True:
    try:
        num1 = input("Please enter the first integer('quit' to quit):\t")
        if num1 == 'quit':
            break
        num2 = input("Please enter the second integer('quit' to quit):\t")
        if num2 == 'quit':
            break
        num1 = int(num1)
        num2 = int(num2)
    except ValueError:
        print("Sorry, please enter an integer")
    else:
        print(str(num1 + num2))

10-8 貓和狗

try:
    with open("cats.txt") as cats:
        for line in cats:
            print(line.rstrip())
except FileNotFoundError:
    print("File not found!(cats.txt)")

try:
    with open("dogs.txt") as dogs:
        for line in dogs:
            print(line.rstrip())
except FileNotFoundError:
    print("File not found!(dogs.txt)")

10-9 沉默的貓和狗

try:
    with open("cats.txt") as cats:
        for line in cats:
            print(line.rstrip())
except FileNotFoundError:
    pass

try:
    with open("dogs.txt") as dogs:
        for line in dogs:
            print(line.rstrip())
except FileNotFoundError:
    pass

10-10 常見單詞

try:
    with open("book.txt") as book:
        bookStr = book.read()
        print(str(bookStr.lower().count('the')))
except FileNotFoundError:
    pass

10-11 喜歡的數字

# favorite_num_dump.py
import json

file_name = "favorite_num.json"
try:
    num = int(input("Please enter your favorite integer:\t"))
except ValueError:
    print("Error! Not a integer!")
else:
    with open(file_name, 'w') as file_obj:
        json.dump(num, file_obj)
# favorite_num_load.py
import json

file_name = "favorite_num.json"
try:
    with open(file_name) as file_obj:
        num = json.load(file_obj)
    print("I know your favorite number! It's " + str(num) + ".")
except FileNotFoundError:
    print("File not found!(favorite_num.json)")

10-12 記住喜歡的數字

import json

file_name = "favorite_num.json"
try:
    with open(file_name) as file_obj:
        num = json.load(file_obj)
except FileNotFoundError:
    try:
        num = int(input("Please enter your favorite integer:\t"))
    except ValueError:
        print("Error! Not a integer!")
    else:
        with open(file_name, 'w') as file_obj:
            json.dump(num, file_obj)
else:
    print("I know your favorite number! It's " + str(num) + ".")

10-13 驗證使用者

import json


def get_stored_username():
    filename = 'username.json'
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return username


def get_new_username():
    username = input("What is you name?\t")
    filename = 'username.json'
    with open(filename, 'w') as f_obj:
        json.dump(username, f_obj)
    return username


def greet_user():
    username = get_stored_username()
    if username:
        print("Username:\t" + username)
        message = "Is this user name right?(Yes/No)\t"
        ok = input(message)
        if ok == 'Yes':
            print("Welcome back, " + username + "!")
        else:
            username = get_new_username()
            print("We'll remember you when you come back, " + username + "!")
    else:
        username = get_new_username()
        print("We'll remember you when you come back, " + username + "!")


greet_user()