在cod中使用return优化python代码

2024-09-27 09:35:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想优化我的代码,使其在代码的某个部分能够很好地返回

我知道我可以用do-while来做,但我认为如果用很多do-while可能会很沉重

我确定我要在评论中返回的位置

你能给我一个解决问题的办法吗

#return here 0
mainMenu()
choix = input()

if choix == 1:
    print "Enter your username"
    username = raw_input()
    print "Enter your password"
    password = raw_input()
    createAccount.createAccount(username,password)
    #return here 1
    educatorMenu()

    choix = input()

    if choix == 1: 
        print "Enter his age"
        ageChild = raw_input()
        print "Enter his photo"
        photoChild = raw_input()
        educator.createChildAccount(ageChild, photoChild)

    elif choix == 2:
        childrenList = educator.seeChildrenList()
        #Return to where it is written "return here 0"

    elif choix == 3:
        childrenList = educator.seeChildrenList()
        print "Associate children?"
        choixEnfant = input()
        educator.associateChildren(childrenList,choixEnfant-1, educator.getIdEducator(username))
        #Return to where it is written "return here 1"

非常感谢


Tags: 代码inputrawreturnifhereusernamepassword
1条回答
网友
1楼 · 发布于 2024-09-27 09:35:07

注意:我不会重新编写/优化您的代码,因为我觉得这样做应该是您的任务。此外,重写它还缺少相当多的函数和引用;您没有交付MWE

因此,您可能会发现这是一个有用的起点,并尝试调整此(非常基本结构)以满足您的需要。然而,请注意:这也远远不够有效,但它将帮助您扩展基本知识

def menu_main():
    q = ['What would you like to do?', 
         '1: List educators',
         '2: Create educator',
         '3: Manage educator\'s children',
         '4: Exit'].join('\n  ')
    return int(input(q).strip()[0])

def menu_educator_create():
    usr = input('Enter username:').strip()
    pwd = input('Enter password:').strip()
    return (usr, pwd)

def menu_educator_manage():
    q = ['What would you like to do?', 
         '1: List children',
         '2: Add child',
         '3: Return'].join('\n  ')
    return int(input(q).strip()[0])

if __name__ == '__main__':
    edus = {}

    exit = False
    while not exit:
        print('===')
        cmd = menu_main()

        if cmd == 1:
            # List educators
            pass
        elif cmd == 2:
            # Create educator
            usr, pwd = menu_educator_create()
            pass
        elif cmd == 3:
            # Manage children
            # (should somehow obtain current educator first)
            exit_edu = False
            while not exit_edu:
                subcmd = menu_educator_manage()
                if subcmd == 1:
                    # List children
                    pass
                elif subcmd == 2:
                    # Add child
                    pass
                elif subcmd == 3:
                    # Return
                    exit_edu = True
                else:
                    # None of the above
                    print('Unknown choice from educator menu.')
        elif cmd == 4:
            # Exit
            exit = True
        else:
            # None of the above
            print('Unknown choice from main menu.')

相关问题 更多 >

    热门问题