Python编程:从入门到实践的动手试一试答案(第十章)

#10-1 Python学习笔记
with open('learning_python.txt') as file_object:
    contents = file_object.read()
    print(contents)

----------------------------------------------
with open('learning_python.txt') as file_object:
    for line in file_object:
        print(line.rstrip())

----------------------------------------------
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:
    line1 = line.replace('Python','C')
    print(line1.rstrip())
#10-3 访客
filename = 'guest.txt'
with open(filename,'w') as file_object:
    name = input('请输入你的名字:')
    file_object.write(name)
#10-4 访客名单
filename = 'guest_book.txt'
with open(filename,'a') as file_object:
    print('按‘q’推出')
    while True:
        name = input('请输入你的名字:')
        if name == 'q':
            break
        file_object.write(name + '\n')
#10-5 关于编程的调查
filename = 'reason.txt'
with open(filename,'a') as file_object:
    print('按‘q’推出')
    while True:
        reason = input('请问你为何喜欢编程:')
        if reason == 'q':
            break
        file_object.write(reason + '\n')
#10-6 加法运算
try:
    number1 = int(input('请输入一个数字:'))
    number2 = int(input('请在输入一个数字:'))
    result = number1 + number2
    print(result)
except ValueError:
    print('你输入的格式有问题')
#10-7 加法计算器
while True:
    try:
        number1 = int(input('请输入一个数字:'))
        number2 = int(input('请在输入一个数字:'))
        result = number1 + number2
        print(result)
    except ValueError:
        print('你输入的格式有问题')
#10-8 猫和狗
try:
    with open('cats.txt') as cat:
        print(cat.read())
    with open('dogs.txt') as dog:
        print(dog.read())
except FileNotFoundError:
    print('对不起没有找到文件')
#10-9 沉默的猫和狗
try:
    with open('cats.txt') as cat:
        print(cat.read())
    with open('dogs.txt') as dog:
        print(dog.read())
except FileNotFoundError:
    pass
#10-10 常见单词
try:
    with open("The Girl Scouts' Canoe Trip by Edith Lavell.txt") as book:
        content = book.read()
        number = content.lower().count('the')
        print(number)
except FileNotFoundError:
    pass
#10-11 喜欢的数字
import json

filename = 'numbers.json'
number = input('请输入你最喜欢的数字:')
with open(filename, 'w') as f_obj:
     json.dump(number, f_obj)
with open(filename) as f_obj:
     number = json.load(f_obj)
     print('I know your favorite number! It is ___' + number)
#10-12 记住喜欢的数字
import json

filename = 'numbers.json'
try:    
    with open(filename) as f_obj:
        number = json.load(f_obj)
        print('I know your favorite number! It is ___' + number)
except FileNotFoundError:
    number = input('请输入你最喜欢的数字:')
    with open(filename, 'w') as f_obj:
        json.dump(number, f_obj)
#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 your name? ")
    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:
        yep = input('Is ' + username + ' your name?(y/n)')
        if  yep == 'y':
            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()
    原文作者:叫我小红
    原文地址: https://blog.csdn.net/weixin_39449570/article/details/78424895
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞