第十章课后作业

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值