0

I'm attempting to set up a system where if the user has seen a 'file' as such they can return to the menu. Why isn't this user defined function making the code loop back to the menu?

My code:

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()

I'm fairly new to coding so don't really know where to start.

This is the error message I'm confronted with:

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
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • Well firstly you used a capital "I" for "Back=Input()". input is lowercase – Neil May 23 '19 at 20:14
  • You normally shouldn't define functions inside an `if` statement. – Barmar May 23 '19 at 20:16
  • What is "Users/feerobinson/Database.py" ? Is this the whole file you are running? Or are you importing something else? – Neil May 23 '19 at 20:16
  • 3
    The error message is about a line `while True:`, but that's not anywhere in the code you posted. – Barmar May 23 '19 at 20:17
  • @Barmar is correct although in Python it is completely valid code to do so. You would need an indentation block after the "def" thought. – Neil May 23 '19 at 20:17
  • @Neil That's why I said "normally" and "shouldn't". There's no reason to do it here. – Barmar May 23 '19 at 20:18
  • Other thing that has to be mentioned, you are doing a recursive call here. If that isn't limited you will get... a stackoverflow :) – Neil May 23 '19 at 20:18
  • @Barmar I know... I said you are correct... Just adding that that particular mistake wouldn't cause a syntax error. – Neil May 23 '19 at 20:19
  • Yes, please don't use a recursive call (calling `menu()` inside `def menu():`) to "restart" - it's a bad practice that will lead to debugging problems and possibly crashes. Just use a loop. – Blorgbeard May 23 '19 at 20:19
  • You might find this useful reading: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – glibdud May 23 '19 at 20:38

1 Answers1

0

There were few mistakes, and the function call to menu() was missing. This should work:

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()