Python Forum
when calling module comes up with syntax error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
when calling module comes up with syntax error
#1
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.
Reply
#2
Please post the full error traceback (verbatim), it contains important information.
Reply
#3
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
Reply
#4
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()
Reply
#5
Thank you - my brain stopped working - Should the main() always be at the end of the program or does it not matter?
Reply
#6
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,181 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 381 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,586 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,228 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  pyscript index error while calling input from html form pyscript_dude 2 980 May-21-2023, 08:17 AM
Last Post: snippsat
  syntax error question - string mgallotti 5 1,316 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,258 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 892 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,851 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,356 May-18-2022, 06:50 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020