def main():
#These are the two options available
print ("1: register")
print("2: login")
signIn = int(input("Do you have an existing account? 1 or 2?"))
#yes = Y
#no = N
if signIn == 2:
existingUser()
elif signIn ==1:
newUser()
main()
existingUser():
print ("signin")
newUser():
print ("new user")
#Message - the script above will not allow me to progress as when I run, it comes up with a syntax error when I try to call the existingUser or newUser module. I cannot see the syntax error.
Please post the full error traceback (verbatim), it contains important information.
def main():
#These are the two options available
print ("1: register")
print("2: login")
signIn = int(input("Do you have an existing account? 1 or 2?"))
#yes = Y
#no = N
if signIn == 2:
existingUser()
elif signIn == 1:
newUser()
main()
existingUser():
print ("signin")
newUser():
print ("new user")
#message It doesn't come up with an error message just a box saying 'invalid syntax' and the double colon after existingUser()is highlighted
This is
existingUser():
SyntaxError.
Has to be a function def.
def existingUser():
print('existingUser Got called')
def newUser():
print('newUser Got called')
def main():
#These are the two options available
print ("1: register")
print("2: login")
signIn = int(input("Do you have an existing account? 1 or 2?"))
#yes = Y
#no = N
if signIn == 2:
existingUser()
elif signIn ==1:
newUser()
main()
Thank you - my brain stopped working - Should the main() always be at the end of the program or does it not matter?
Not a hard and fast rule, but it will usually end up that way. Remember you cannot call a function until you define it, hence @snippsat's reply.
If you take your original code (with corrections):
def main():
# These are the two options available
print("1: register")
print("2: login")
signIn = int(input("Do you have an existing account? 1 or 2?"))
# yes = Y
# no = N
if signIn == 2:
existingUser()
elif signIn == 1:
newUser()
main()
def existingUser():
print("signin")
def newUser():
print("new user")
You will get a NameError:
Error:
1: register
2: login
Do you have an existing account? 1 or 2?1
Traceback (most recent call last):
File "C:/Python/Sound/scratch2.py", line 20, in <module>
main()
File "C:/Python/Sound/scratch2.py", line 17, in main
newUser()
NameError: name 'newUser' is not defined
Process finished with exit code 1
That is because to Python, newUser has not been defined yet (and neither does existingUser). Since you are calling them within the main function, they must exist
before the main function.