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

    Join Date
    Jul 2006
    Posts
    6
    Rep Power
    0

    Syntax Error: Invalid Token


    Ok guys, I am following from my book and I copied this exactly from it. I have looked over to make sure everything is typed out properly, and to me it is. This my my code:

    Code:
    #
    #  Word Problems.py
    #  Demonstrates numbers and math
    #  
    #  Created by Jonathan Stuparyk-Musso on 07/07/06.
    #  Copyright (c) 2006 __Appleukit__. All rights reserved.
    # 
    print \
    """
    If a pregnant hippo, weighing 2,000 pounds, gives birth to a 100 pound calf,
    but then eats 50 pounds of food, how much does she weigh?"""
    raw_input("Press the enter key to find out.")
    print "2000 - 100 + 50 ",
    print 2000 - 100 + 50
    
    print \
    """
    If an adventurer returns from a successful quest and buys each of 6 companions
    3 bottles of ale, how many bottles does the adventurer buy?"""
    raw_input("Press the enter key to find out.")
    print "6 * 3 = ",
    print 6 * 3
    
    print \
    """
    If a kid has 24 pieces of Halloween candy and eats 6 pieces a day, how many days
    will the stash last?"""
    raw_input("Press the enter key to find out.")
    print "24 / 6 = ",
    print 24 / 6
    
    print \ 
    """
    If a group of 4 pirates finds a chest full of 107 gold coins, and they divide the booty
    evenly, how many coins will be left over?"""
    raw_input("Press the enter key to find out.")
    print "107 % 4 =",
    print 107 % 4
    
    print \
    """
    If a restaurant check comes to 19 dollars with tip, and you and your friends split it evenly
    4 ways, how much do you need to throw in?"""
    raw_input("Press the enter key to find out.")
    print "19 / 4 = ",
    print 19 / 4
    print "WRONG!"
    raw_input("Press the enter key for the right answer.")
    print 19.0 / 4
    
    raw_input("\n\nPress the enter key to exit.")
    When I go to run it in the terminal, this is the error I get:

    File "/Users/Jonathan/Documents/Python Scripts/wordproblems.py", line 32
    print \
    ^
    SyntaxError: invalid token
    Now I don't see what the problem is since line 32 is:

    Code:
    print \
    Any help is appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Whoa! This one was hard to find There's a space behind the "\". Delete the space and it works

    Regards, mawe
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    608
    Rep Power
    65
    Nice to have an editor that has a show-white-space option.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    6
    Rep Power
    0
    I am currently using xCode on OS X Tiger. Thank you for your help, was REALLY stumped on that one, lawl.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Thanks


    It may be 2013, and this post is from 2006 but I had the exact same issue, I'm glad google can find old forum posts ha.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    475
    Rep Power
    33
    In that case, for future searches, the "\" is not necessary in Python.
    Code:
    print """If a group of 4 pirates finds a chest full of 107 gold coins, and they divide the booty
    evenly, how many coins will be left over?"""
    And the answer is no coins are left over. The biggest, baddest pirate threatens to run through any one who tries to stop him from taking the extra coins-pirates don't leave leftover gold coins.
    Last edited by dwblas; June 19th, 2013 at 05:16 PM.

IMN logo majestic logo threadwatch logo seochat tools logo