python - 如何压缩重复的 Python 代码?
问题描述
我正在制作一个程序,它可以在不添加任何有助于 gui 的库的情况下做笔记。该程序目前为 163 行,我认为它太长以至于无法阅读。我的程序中的指南变得重复,我该如何压缩它?
这是代码。
note_control = 1
modeError = 'modeError: Mode entered does not exist. Please input the correct (spelling of the) mode.'
while note_control == 1:
print ('Please enter mode. Type /help for guide.')
mode=input ('- ')
mode_list = mode.split (' ')
mode_name = mode_list [0]
if mode_name == '/help':
legend = ('Legend:\n' + '\033[1;31;40mOptional Text\033[0;37;40m\n' + '\033[1;33;40mNecessary, but needs to be filled in for better results.\033[0;37;40m\n' + '\n')
append = ('/append: Attaches a subnote to a note. [/append notename.extension subnotename]')
create = ('/create: Creates a note. [/create \033[1;33;40mnotename\033[1;31;40m.extension\033[0;37;40m]\n')
end = ('/end: Ends the creation of a note. Cannot be used with /help. Use /help create for guide. [/end]\n')
help = ('/help: Opens the guide. [/help \033[1;31;40mmodename\033[0;37;40m]\n')
rename = ('/rename: Renames a note. [/rename old_file_name.extension new_file_name.extension]\n')
quit = ('/quit: Stops the program. [/quit]\n')
if mode == '/help':
print (legend + append + create + end + help + rename + quit)
elif mode == '/help append':
print (legend + append)
elif mode == '/help create':
print (legend + create + end)
elif mode == '/help help':
print (legend + help)
elif mode == '/help rename':
print (legend + rename)
elif mode == '/help quit':
print (legend + quit)
else:
print (modeError)
else:
print (modeError)
print ()
提前感谢您的回答!
解决方案
避免大量if/elif
重复的一个好技巧是将内容放入 dict 中,以便您可以使用查找来关联两个值:
help_legend = """Legend:
\033[1;31;40mOptional Text\033[0;37;40m
\033[1;33;40mNecessary, but needs to be filled in for better results.\033[0;37;40m
"""
mode_error = ('modeError: Mode entered does not exist. '
'Please input the correct (spelling of the) mode.\n')
command_help = {
'append': '/append: Attaches a subnote to a note. '
'[/append notename.extension subnotename]',
'create': '/create: Creates a note. '
'[/create \033[1;33;40mnotename\033'
'[1;31;40m.extension\033[0;37;40m]\n'
'/end: Ends the creation of a note. Cannot be used with /help. '
'Use /help create for guide. '
'[/end]\n',
'help': '/help: Opens the guide. '
'[/help \033[1;31;40mmodename\033[0;37;40m]\n',
'rename': '/rename: Renames a note. '
'[/rename old_file_name.extension new_file_name.extension]\n',
'quit': '/quit: Stops the program. '
'[/quit]\n',
}
while True:
mode = input('\nPlease enter mode. Type /help for guide.\n- ').split()
if not mode or mode[0] != '/help':
print(mode_error)
continue
if len(mode) < 2:
# Show the help for all of the commands.
help_text = ''.join(command_help.values())
elif mode[1] in command_help:
# Show the help for the specific command entered.
help_text = command_help[mode[1]]
else:
print(mode_error)
continue
print(help_legend + help_text)
推荐阅读
- apache-kafka - 有没有办法通过带有内置 SMT 的 Kafka Connect Config 根据某个字段上的某些条件将记录值设为空?
- google-sheets - ARRAYFORMULA 中的查询以过滤和连接
- ios - Mac Catalyst 场景状态和布局
- php - 如何在 Laravel 中将图像从 SVG 转换为 PNG
- reactjs - 比较 2 个数组,如果 id 匹配,我们需要启用单选按钮(如果没有禁用)
- python - 为什么 models.BooleanField 返回字节而不是布尔值?
- swiftui - 选择新标签时如何更新导航标题?
- proxy - 根据请求路径等特定条件更改响应
- visual-studio-code - 如何更改 LF 的可视代码标准 CRLF?
- python - 具有调用和最小化其他函数的函数的 Python 类