Syntax Error

This is the place for queries that don't fit in any of the other categories.

Syntax Error

Postby murster972 » Fri Jul 19, 2013 9:00 pm

i have recently started learning python and decided to try and make a basic program. Every time i try and run it i get a syntax error, i have tried changing different lines of the code but, can't figure out whats wrong. Please help.

Code: Select all
#basic login system

#strings and input for username and password
#login function
def login():
   user_name = raw_input("Please Enter Username: ")
   password = raw_input("Please Enter Password: ")
#if and elif statements for login
if(user_name == "test1" and password == "test1"):
   print"Succesful Login"
elif(user_name == "test1" and password == "test1"):
   print"Incorrect Username/Password"
      login()
Last edited by micseydel on Fri Jul 19, 2013 9:12 pm, edited 1 time in total.
Reason: Changed title to something meaningful, locked OP.
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: Syntax Error

Postby micseydel » Fri Jul 19, 2013 9:14 pm

In Python, indentation matters, and changes between lines have meaning. The last line of the code you posted is indented compared to the one before when it should not be. This is so wrong that it is a syntax error rather than an error which would happen at run time. If the fix is not obvious, post back, and provide as much description as you can as to what you think your code does and why it should be working.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1128
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Syntax Error

Postby murster972 » Fri Jul 19, 2013 10:15 pm

thanks for the help.i dont understand what the error, i thought it would of worked.
i thought it would run a simple login. PLease help.
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: Syntax Error

Postby micseydel » Fri Jul 19, 2013 10:20 pm

Make your last line have the same indentation level as the line before it. Do you see why this is how it should be? Why did you think the extra indentation was ok/good?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1128
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Syntax Error

Postby murster972 » Fri Jul 19, 2013 10:21 pm

the command window says:
print"succesful Login"

SyntaxError: invalid syntax
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: Syntax Error

Postby murster972 » Fri Jul 19, 2013 10:22 pm

the last lanuage i learnt was batch, the indents did not matter, also they didnt matter in HTML.
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: Syntax Error

Postby murster972 » Fri Jul 19, 2013 10:25 pm

Please code you correct my code ?
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: Syntax Error

Postby Jaro » Fri Jul 19, 2013 10:48 pm

murster972 wrote:the command window says:
print"succesful Login"

SyntaxError: invalid syntax


That line looks ok. At least in Python 2. Are you sure you are not using Python 3? In that case it should be:

Code: Select all
print("succesful Login")
Last edited by Jaro on Fri Jul 19, 2013 10:48 pm, edited 1 time in total.
Code: Select all
<function signature at 0xb73f910c>
User avatar
Jaro
 
Posts: 8
Joined: Sat Feb 23, 2013 6:16 pm

Re: Syntax Error

Postby snippsat » Fri Jul 19, 2013 10:48 pm

the last lanuage i learnt was batch, the indents did not matter, also they didnt matter in HTML

In all programming languages indentation matter(even if you don't get a error),you don't see much code without indentation.
So the creator of Python did something smart and did not use extra signs for code block like { },but used indentation to do this.

Code: Select all
def login():
    user_name = raw_input("Please Enter Username: ")
    password = raw_input("Please Enter Password: ")
   
    if user_name == "test1" and password == "test1":
       print"Succesful Login"
    else:
       print"Incorrect Username/Password"

login()
User avatar
snippsat
 
Posts: 132
Joined: Thu Feb 21, 2013 12:04 am

Re: Syntax Error

Postby micseydel » Fri Jul 19, 2013 10:54 pm

Jaro: he's using raw_input.

murster972: Do you have any questions about the code that snippsat posted? Do you see what indentation is for?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1128
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Syntax Error

Postby Jaro » Fri Jul 19, 2013 10:59 pm

micseydel wrote:Jaro: he's using raw_input.


That would be a NameError, and wouldn't even be raised at runtime, since with his indentation, that section of code wouldn't be executed.
Code: Select all
<function signature at 0xb73f910c>
User avatar
Jaro
 
Posts: 8
Joined: Sat Feb 23, 2013 6:16 pm

Re: Syntax Error

Postby micseydel » Sat Jul 20, 2013 12:29 am

Perhaps he's using Python 3 on his system and that's part of the problem here, but the existence of raw_input() in his code implied to me that he's using Python 2.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1128
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Syntax Error

Postby murster972 » Sat Jul 20, 2013 7:49 am

thank you for the help. I understand what i did wrong and will learn from this mistake next time, and thanks for the corrected code snippsat.
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 9 guests