gin()
exam_generator = Exam_generator(grade)
else:
print("请输入10-30之间的数字")
except ValueError:
print("请输入10-30之间的数字或切换为小学、初中和高中三个选项中的一个!")
if __name__ == '__main__':
main()
accounts.json
{
"小学": [
{
"account": "张三1",
"password": "123"
},
{
"account": "张三2",
"password": "123"
},
{
"account": "张三3",
"password": "123"
}
],
"初中": [
{
"account": "李四1",
"password": "123"
},
{
"account": "李四2",
"password": "123"
},
{
"account": "李四3",
"password": "123"
}
],
"高中": [
{
"account": "王五1",
"password": "123"
},
{
"account": "王五2",
"password": "123"
},
{
"account": "王五3",
"password": "123"
}
]
}
优点
模块化设计: 代码使用了模块化的设计,将不同功能的代码分别放在了不同的模块(account
? 和 examgenerator
?)中,提高了代码的可维护性和可重用性。
Google代码规范
大体遵守了Google Python代码规范:
1. 模块和函数命名
代码中的模块和函数命名在大多数情况下是清晰和符合规范的。例如,Accounts
? 类和 ExamGenerator
? 抽象类的命名是符合规范的。
2. 函数参数类型注释
在一些方法中,使用了参数类型的注释,这有助于理解参数的预期类型。这是符合Google代码规范的一项实践。
def generate(self, num_range: tuple) -> str:
"""
Generate a math problem.
"""
...
def check_repeat(self, account: str, prob: str) -> bool:
"""Check if the prob is repeated.
Arguments:
account: The account using the program.
prob: The prob str to check.
Returns:
True if the prob is repeated, otherwise False.
"""
...
3. 异常处理
码中有一些异常处理,这是符合Google代码规范的一项实践。异常处理有助于处理潜在的错误情况。
try:
# 异常处理代码
except ValueError:
print("请输入10-30之间的数字或切换为小学、初中和高中三个选项中的一个!")
也有值得改进的地方:
文档字符串(Docstrings)
代码中缺少文档字符串(Docstrings)。文档字符串是对模块、类、函数和方法功能的详细描述,以及参数和返回值的说明。这是Google代码规范强烈鼓励的一项实践,有助于提高代码的可读性和可维护性。
class Account(object):
"""Account class.
Attributes:
account: The account, such as '张三1'.
password: The password of the account.
grade: The corresponding grade of the account, to generate exam with
different difficuty.
"""
...
class Accounts(object):
"""Accounts class, to manage accounts.
Attributes:
accounts: The accounts list, read from accounts.json.
"""
...
五、总结
第一次写博客评价他人的项目,我深切感受到了写代码还得是一个团队活动,人和人之间交流彼此的意见和经验,以求共同进步,这样的学习方式更加高效。搭档的项目写得很好,相比我写的java而言,代码更简练优美,希望大家有所启发。