1 Replies - 739 Views - Last Post: 17 May 2011 - 08:18 PM Rate Topic: -----

#1 kevinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 29-September 10

new to Python, having trouble with simple function stuff here

Posted 17 May 2011 - 07:07 PM

Hello there. I've started learning Python from Zed A. Shaw's book, "Learn Python the Hard Way." I wrote my own program here because I've just finished exercise 18, where functions are introduced. The program, with code below, doesn't run for some reason. Please help if possible? Using Python 2.7.1 on Windows 7.
I get this error when trying to run it:
File "ex18.py", line 10
yn2=gn2()
^
SyntaxError: invalid syntax

def getname():
  print "Hello!"
  return raw_input("Please enter your name:")
yn=getname()
print "Hello, %s." % (yn)
gn2()
def gn2():
  print "Give me your name again."
  return raw_input("Enter it again, %s." % (yn)
yn2=gn2()
print "Hello again, %s." % (yn2)

This post has been edited by atraub: 17 May 2011 - 08:18 PM
Reason for edit:: Added code tags for you :)


Is This A Good Question/Topic? 0
  • +

Replies To: new to Python, having trouble with simple function stuff here

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: new to Python, having trouble with simple function stuff here

Posted 17 May 2011 - 08:18 PM

In gn2, your raw_input is missing its' closing paren. Here's the code cleaned up a little and working properly. As a rule of thumb, use 4 spaces for your indentation as that's the generally accepted standard:

def getname():
    print "Hello!"
    return raw_input("Please enter your name:")

def gn2():
   print "Give me your name again."
   return raw_input("Enter it again, %s." % (yn))


yn = getname()        
print "Hello, %s." % (yn)
yn2=gn2()
print "Hello again, %s." % (yn2)



EDIT:
I know you're new to functions, but you should parameterize the second function to take the name as an input. That way, you won't have to run getname() before gn2().

Also, because I did mention coding standards... the naming standard for variables is camelCase and the standard for function names is words_separated_by_spaces :)

EDIT2:
Kev, this isn't the first time you've forgotten to use code tags. Posted Image Here is a video tutorial explaining how to add code tags to your post.

This post has been edited by atraub: 17 May 2011 - 08:30 PM
Reason for edit:: No one can ever accuse me of not being thorough!!!!!!

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1