【高级编程技术】【作业】【第五周】【2】

教材第10章课后练习

10-1 Python学习笔记

learning_python.txt

In Python you can write hello world.
In Python you can change the type of variables.
In Python you can write less code with the same function.
In Python you can write interesting programs.

main.py

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:
    print(file_object.read().replace('Python', 'C'))

10-3 访客

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

10-4 访客名单

with open('guest_book.txt', 'w') as file_object:
    while True:
        name = input('Please input your name: ')
        print('Hello,', name)
        file_object.write(name+' has arrived.\n')

10-5 关于编程的调查

while True:
    reason = input('Why do you like programming? ')
    with open('reasons.txt', 'a') as file_object:
        file_object.write(reason+'\n')

10-6 加法运算

try:
    num1 = int(input('Please input an integer: '))
    num2 = int(input('Please input an integer: '))
    print('The sum is', num1+num2)
except ValueError:
    print("Sorry, it seems that you did not input an integer.")

10-7 加法计算器

while True:
    try:
        num1 = int(input('Please input an integer: '))
        num2 = int(input('Please input an integer: '))
        print('The sum is', num1+num2)
    except ValueError:
        print("Sorry, it seems that you did not input an integer.")

10-8 猫和狗

try:
    with open('cats.txt') as file_object:
        cats = file_object.readlines()
    for line in cats:
        print(line.rstrip())
except FileNotFoundError:
    print('cats.txt not found')
try:
    with open('dogs.txt') as file_object:
        dogs = file_object.readlines()
    for line in dogs:
        print(line.rstrip())
except FileNotFoundError:
    print('dogs.txt not found')

10-9 沉默的猫和狗

try:
    with open('cats.txt') as file_object:
        cats = file_object.readlines()
    for line in cats:
        print(line.rstrip())
except FileNotFoundError:
    pass
try:
    with open('dogs.txt') as file_object:
        dogs = file_object.readlines()
    for line in dogs:
        print(line.rstrip())
except FileNotFoundError:
    pass

10-10 常见单词

文件:http://www.gutenberg.org/cache/epub/29444/pg29444.txt

with open('pg29444.txt') as file_object:
    print(file_object.read().lower().count('the'))

10-11 喜欢的数字

dump.py

import json
number = input('Please input your favourite number: ')
with open('number.json', 'w') as file_object:
    json.dump(number, file_object)

load.py

import json
with open('number.json') as file_object:
    number = json.load(file_object)
message = "I know your favourite number! It's xxx.".replace('xxx', str(number))
print(message)

10-12 记住喜欢的数字

import json
try:
    with open('number.json') as file_object:
        number = json.load(file_object)
    message = "I know your favourite number! It's xxx.".replace('xxx', str(number))
    print(message)
except FileNotFoundError:
    number = input('Please input your favourite number: ')
    with open('number.json', 'w') as file_object:
        json.dump(number, file_object)

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:
        check = input('Are you xxx?(Y/N) '.replace('xxx', username))
        if check.upper() == 'Y':
            print('Welcome back, ' + username + '!')
            return
    username = get_new_username()
    print("We'll remember you when you come back, "
        + username + "!")

greet_user()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值