#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    temporarly: Rosetown, Sk (Canada)
    Posts
    22
    Rep Power
    0

    Help with Python syntax error


    I tiped in this piece of code:

    Code:
    def EvenOdd(z):
        if z% == 0:
            print z, 'is even'
        else:
            print z, 'is odd'
    and I am getting an invalid syntax error! Why? The mark is at the "==". Is this moderator not available in Python 2.4?
  2. #2
  3. Retired Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    London, UK
    Posts
    6,669
    Rep Power
    147
    Thread moved from Beginner Programming to Python Programming
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    "%" is an operator that requires two numbers:
    Code:
    def EvenOdd(z):
        if z%2 == 0:
            print z, 'is even'
        else:
            print z, 'is odd'
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    temporarly: Rosetown, Sk (Canada)
    Posts
    22
    Rep Power
    0
    Ouch!
    I should have seen that myself! But Thanks anyways...
    By the way, is there any way, that I can use a local variable from a function in the main program? I tried the return statement, but it does not work.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    No...

    Simple way using return:

    Code:
    def addNumbers(a, b):
        answer = a+b
        return answer
    
    print "Four plus three is..."
    answer = addNumbers(4, 3)
    print answer
    i.e. make a new variable also called answer.

    Uglier, not recommended way: global variables

    Code:
    global x
    x = 10
    
    print x
    
    def someFunction():
        global x
        print x
        x += 1
    
    print x
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    temporarly: Rosetown, Sk (Canada)
    Posts
    22
    Rep Power
    0

    Next problem ;)


    There is something strange about this code...

    Code:
    def print_forward(string):
        x = 0
        while x <= len(string):
           print string[x]
           x = x + 1
    
    print_forward('Hello')
    It actually prints out the 'Hello' as wanted (one letter per line), but then it gives me this error message:

    Code:
    IndexError: string index out of range
    (the error message is for the code it actually ran)
    and terminates the program. Why?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Well, what is happing is the array doesn't have that meany things in it. So when the computer tries to access something that isn't indexed in the array witch is what your code was doing it gets that error

    here is working v. of your code

    Code:
    def print_forward(string):
     	x = 0
     	while x < len(string): #to fix it i just removed the = sign so it wont try to acces anything more
     	   print string[x]
     	   x++ # i did this because it shorter and more to the point it has the same affect of x = x + 1
     
     
     print_forward('Hello')
    now if this doesn't make since the reason is, len doesn't start at 0 and an index of an array does. So the code is going 1 more than needed.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    temporarly: Rosetown, Sk (Canada)
    Posts
    22
    Rep Power
    0
    Thanks a lot.
    I always thought, that all string operators started counting at 0... Are there more of the 'len' kind?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Originally Posted by CyBerHigh
    Well, what is happing is the array doesn't have that meany things in it.
    This would happen no matter how many things were in the list.

    Code:
    here is working v. of your code
     
      
    Code:
    x++ # i did this because it shorter and more to the point it has the same affect of x = x + 1
    It doesn't. x = x + 1 adds one to the value referenced by x, whereas x++ raises a SyntaxError. x+=1 is the closest thing I know of in Python.

    I always thought, that all string operators started counting at 0... Are there more of the 'len' kind?
    What's happening is the confusion that occurs when you mix 0 based indexing, and counting:

    Code:
    "abc" is a string with three characters. 
    
    >>> len("abc")
    3
    
    # indexing starts at zero:
    "abc"
     012
    
    >>> "abc"[0]
    "a"
    
    >>> "abc"[1]
    "b"
     
    >>> "abc"[2]
    "c"
    
    >>> while x <= len(string):  # your counter has gone 0, 1, 2 and got a, b, c.
    x is now three, equal to the length of the string, so it carries on. It tries to get "abc"[3], which doesn't exist.

    Instead, don't use indexes into a list unless you need to (and you rarely do), use iteration:

    Code:
    for x in string:
      print x

    Comments on this post

    • netytan agrees
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    o man, sorry, i forgot to test that change. I am thinking perl Sorry baout that

    but the array thing is still right

IMN logo majestic logo threadwatch logo seochat tools logo