首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python历史测验程序

Python历史测验程序
EN

Code Review用户
提问于 2020-10-17 07:53:27
回答 1查看 213关注 0票数 4

这个程序允许用户管理来自多年历史事件的问题和测试用户。我正在寻找关于如何更好地组织代码的建议,可能会将OOP应用于函数,并避免使用全局变量data_filename

代码语言:javascript
运行
复制
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from enum import Enum
import os
import pickle
import random


data_filename = 'program.obj'


def read_int(prompt='> ', errmsg='Invalid number!'):
    number = None
    while number is None:
        try:
            number = int(input(prompt))
        except ValueError:
            print(errmsg)
    return number


def display_menu():
    print('What do you want to do?')
    print('[1] List all historical events')
    print('[2] Add event')
    print('[3] Remove event')
    print('[4] Quiz')
    print('[5] Statistics')
    print('[6] Clear statistics') # TODO: move to options submenu
    print('[7] Exit')


def clear_screen():
    print(chr(27) + "[2J")


def pause():
    input('Press any key to continue...')


def yes_or_no(prompt='Proceed? [y|n]\n> ', errmsg='Valid answers are y and n.'):
    answer = input(prompt).strip().lower()
    while answer != 'y' and answer != 'n':
        print(errmsg)
        answer = input(prompt).strip().lower()
    return answer


def read_data_file():
    file = open(data_filename, 'a+b')
    file.seek(0)
    data = {'events': [], 'statistics': {'total_successes': 0, 'total_failures': 0}}
    if os.path.getsize(data_filename) > 0:
        data = pickle.load(file)
    file.close()
    return data


def list_events(data):
    events = data['events']
    if len(events) == 0:
        print('Event list is empty!')
    else:
        print('Historical events:')
        for event in events:
            print(event)


def add_event(data):
    events = data['events']
    year = read_int(prompt='Enter year: ')
    description = input('Enter description: ')
    events.append({'year': year, 'description': description})
    print('Successfully added a new historical event!')


def remove_event(data):
    events = data['events']
    if len(events) == 0:
        print('Event list is empty!')
    else:
        for event in events:
            print(events.index(event), event)
        index = read_int('Which event do you want to delete? ')
        try:
            events.pop(index)
            print('Successfully deleted event!')
        except IndexError:
            print('Number out of range!')


def quiz(data):
    events = data['events']
    stats = data['statistics']
    if len(events) == 0:
        print('Event list is empty!')
    else:
        num = read_int(prompt='How many questions should I ask? ')
        if 0 < num <= len(events):
            for event in random.sample(events, num):
                print(event['description'])
                year = read_int('In which year was following event occurred? ')
                if year == event['year']:
                    stats['total_successes'] += 1
                    print('Good answer!')
                else:
                    stats['total_failures'] += 1
                    print('Bad answer!')
        elif num < 0:
            print('Number of questions can\'t be negative!')
        else:
            print('Too much questions!')


def display_stats(data):
    stats = data['statistics']
    tries = stats['total_successes'] + stats['total_failures']
    if tries == 0:
        total_successes = 0
        total_failures = 0
    else:
        total_successes = stats['total_successes']/tries * 100
        total_failures = stats['total_failures']/tries * 100
    print('Statistics')
    print('Total: {0:10.2f}% successes, {1:10.2f}% failures'.format(total_successes, total_failures))


def clear_stats(data):
    answer = yes_or_no('Are you sure you want to clear statistics? [y|n]\n> ')
    if answer == 'y':
        data['statistics'] = {'total_successes': 0, 'total_failures': 0}
        print('Successfully cleared statistics!')
    else:
        print('Statistics left unchanged.')


# TODO: settings submenu
# def settings(data):
#     print('-' * 10)
#     print('Program settings')
#     print('-' * 10)
#     print('[1] Clear statistics')
#     print('[2] Back')
#     user_choice = read_int()
#     while user_choice != 2:
#         if user_choice == 1:
#             clear_stats(data)


def update_data_file(data):
    file = open(data_filename, 'wb')
    pickle.dump(data, file)
    file.close()


class Choices(Enum):
    list_events = 1
    add_event = 2
    remove_event = 3
    quiz = 4
    statistics = 5
    clear_stats = 6
    exit = 7


program_data = read_data_file()
choice = None
while choice != Choices.exit.value:
    clear_screen()
    display_menu()
    choice = read_int()
    if choice == Choices.list_events.value:
        list_events(program_data)
    elif choice == Choices.add_event.value:
        add_event(program_data)
    elif choice == Choices.remove_event.value:
        remove_event(program_data)
    elif choice == Choices.quiz.value:
        quiz(program_data)
    elif choice == Choices.statistics.value:
        display_stats(program_data)
    elif choice == Choices.clear_stats.value:
        clear_stats(program_data)
    elif choice == Choices.exit.value:
        print('Good bye!')
    else:
        print('Invalid choice!')
    update_data_file(program_data)
    pause()
EN

回答 1

Code Review用户

发布于 2020-10-17 13:21:32

常数

data_filename应该大写,因为它是一个全局常量。

早期返回

没有必要使用number作为while的条件,而是:

代码语言:javascript
运行
复制
number = None
while number is None:
    try:
        number = int(input(prompt))
    except ValueError:
        print(errmsg)
return number

可以是

代码语言:javascript
运行
复制
while True:
    try:
        return int(input(prompt))
    except ValueError:
        print(errmsg)

菜单管理

display_menu可以使用一个元组序列,更好的是一个命名元组或@dataclasses序列,每个元组都有一个标题字符串属性和一个可调用属性。那么你的display_menu可能是

代码语言:javascript
运行
复制
print('What do you want to do?')
print('\n'.join(f'[{i}] {item.title}' for i, item in enumerate(menu, 1)))

我看到你也有一个Choices枚举。这还不错,而且你可以同时使用枚举和上面的建议,只要你有一本关于枚举选择功能引用的字典。

成员资格测试

代码语言:javascript
运行
复制
answer != 'y' and answer != 'n'

可以是

代码语言:javascript
运行
复制
answer not in {'y', 'n'}

环结构

避免两次调用input;这是:

代码语言:javascript
运行
复制
answer = input(prompt).strip().lower()
while answer != 'y' and answer != 'n':
    print(errmsg)
    answer = input(prompt).strip().lower()
return answer

可以是

代码语言:javascript
运行
复制
while True:
    answer = input(prompt).strip().lower()
    if answer in {'y', 'n'}:
        return answer
    print(errmsg)

文件操作

代码语言:javascript
运行
复制
file = open(data_filename, 'a+b')
file.seek(0)

有几个问题:

  • 新打开的文件句柄不需要初始查找到开始;如果以读二进制模式打开,则这是多余的。
  • 您应该使用with并避免显式的close()
  • 只有在需要时才需要初始化默认的data
  • 您希望检查文件是否存在,而不是其大小。

所以:

代码语言:javascript
运行
复制
if os.path.exists(data_filename):
    with open(data_filename, 'rb') as file:
        return pickle.load(file)
return {'events': [], 'statistics': {'total_successes': 0, 'total_failures': 0}}

冗余谓词

代码语言:javascript
运行
复制
if 0 < num <= len(events):
    ...
elif num < 0:
    print('Number of questions can\'t be negative!')
else:
    print('Too much questions!')

需要重新考虑一下。首先,too much questions应该是too many questions。另外,如果用户输入0怎么办?当然,这不是“太多的问题”,但这才是要打印的。建议:

代码语言:javascript
运行
复制
if num < 1:
    print('Not enough questions.')
elif num > len(events):
    print('Too many questions.')
else:
    ...

强类型强结构化数据

代码语言:javascript
运行
复制
stats['total_successes'] + stats['total_failures']

就是我见过的所谓的“数据面食”的例子。在这里,当包含类型提示的@dataclass之类的东西更合适时,字典就会被滥用。

字符串插值

代码语言:javascript
运行
复制
'Total: {0:10.2f}% successes, {1:10.2f}% failures'.format(total_successes, total_failures)

更容易表达为

代码语言:javascript
运行
复制
(
    f'Total: {total_successes:10.2f}% successes, '
    f'{total_failures:10.2f}% failures'
)

行分隔是可选的,但对易读性更好。

字符串转义

代码语言:javascript
运行
复制
'Number of questions can\'t be negative!'

更容易写成

代码语言:javascript
运行
复制
"Number of questions can't be negative!"
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/250781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档