-4

So I'm trying to code a login dialogue in the terminal with Python3 but for some reason this code:

def start():

    login_signin_Confirmation = input("Welcome! Do you wish to log-in to your account or sign up to this platform? \n")

    if login_signin_Confirmation == "login":
        
        login()
    else:
        signup()       
start()

def login():

    login_username = input("Great! start by typing in your username.")

    if login_username in users:
        print("Welcome Back, {login_username}")
login()

prompts me this error message:

Welcome! Do you wish to log-in to your account or sign up to this platform? 
login
Traceback (most recent call last):
  File "login.py", line 22, in <module>
    start()
  File "login.py", line 19, in start
    login()
NameError: name 'login' is not defined

Just started out some I'm not really sure what I did wrong and I couldn't find somebody with the same problem...

khelwood
  • 55,782
  • 14
  • 81
  • 108
MinionMax
  • 5
  • 1
  • 3
    You can't run a function that isn't defined yet. You call `start()` before `login` is defined, and `start` tries to call `login()`. – khelwood Sep 03 '20 at 17:05
  • Python is the interpreted language, it's not a compiler, so it can only use function which are defined before it's use – Gahan Sep 03 '20 at 17:09
  • Why the downvote? Question is very valid. It may be a duplicate but one cannot know every question asked in the site. – Asocia Sep 03 '20 at 17:13
  • @Asocia Downvote because "the question does not show any research effort", and a search on Google or SO would return a lot of relevant results. – Pranav Hosangadi Sep 03 '20 at 17:21
  • Also, don't forget to use the proper f-string syntax (the preceding 'f' before string) in the print statement. (or use `.format( ... )` ) – Tibebes. M Sep 03 '20 at 17:21
  • @khelwood Thanks! I've only worked with javascript where calling a function before it's been defined is possible... Got a lot to learn haha – MinionMax Sep 03 '20 at 17:28
  • @Pranav Hosangadi If you look at the title, OP thinks that the problem is something to do with if statement. So searching on google wouldn't help as they will probably use wrong keywords. Anyways, I'm not in position to argue about this. I just wondered why. – Asocia Sep 03 '20 at 21:36

1 Answers1

0

as others pointed out, You need first to define, then to call a function :

# imports first

# now definitions of functions

def start():
    login_signin_Confirmation = input("Welcome! Do you wish to log-in to your account or sign up to this platform? \n")
    if login_signin_Confirmation == "login":
        login()
    else:
        signup()       

def signup():
    pass

def login():
    login_username = input("Great! start by typing in your username.")
    if login_username in users:
        print("Welcome Back, {login_username}")


# main 
def main():
    start()

# guard multiprocess context
if __name__ == '__main__':
    main()

bitranox
  • 1,664
  • 13
  • 21