繁体   English   中英

如何压缩重复的 Python 代码?

[英]How can I compact a repetitive Python code?

我正在制作一个程序,它可以在不添加任何有助于 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM