用户定义函数未循环,如何使其循环?

时间:2019-05-23 20:12:33

标签: python

我正在尝试建立一个系统,如果用户看到这样的“文件”,他们可以返回菜单。为什么用户定义的功能不能使代码循环回到菜单?

我的代码:

print('Please input username, if you have not got an assigned username, please contact ')
Username=input()
if Username =='Frobinson':
    print('Please input password, the default password will be QWERTY')
    Password=input()
    if Password == 'QWERTY':
        def menu():
        print('-----------------------------')
        print('Options:')
        print('A: See files')
        optionselect=input()
        if optionselect =='A':
            print('Enter a name to see details')
            name=input()
            if name== 'Archie_Rayner':
                print()
                print()
                print('Profile for Archie Rayner')
                print('-----------------------------')
                print('Name: Archie Rayner')
                print('Year: 11')
                Back=Input()
                if Back='Back':
                    menu()

我对编码还很陌生,所以真的不知道从哪里开始。

这是我遇到的错误消息:

Traceback (most recent call last):
  File "/Volumes/Thonny 3.1.2/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "/Users/feerobinson/Database.py", line 9
    while True:
        ^
IndentationError: expected an indented block

1 个答案:

答案 0 :(得分:0)

几乎没有错误,并且对menu()的函数调用丢失了。 这应该起作用:

print('Please input username, if you have not got an assigned username, please contact ')
Username=input()
if Username =='Frobinson':
    print('Please input password, the default password will be QWERTY')
    Password=input()
    if Password == 'QWERTY':
        def menu(): # indenting the function definition inside the if statement
            print('-----------------------------')
            print('Options:')
            print('A: See files')
            optionselect=input()
            if optionselect =='A':
                print('Enter a name to see details')
                name=input()
                if name== 'Archie_Rayner':
                    print()
                    print()
                    print('Profile for Archie Rayner')
                    print('-----------------------------')
                    print('Name: Archie Rayner')
                    print('Year: 11')
                    Back=input() # input() instead of Input()
                    if Back=='Back': # == instead of =
                        menu()
        menu() # function call to menu()