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

    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Newbe: got an error, could someone explain this to me?


    Hey all,

    I just got the book Python for Dummies and I started learning.
    Now I got stuck. So if someone could help me, that would be great

    This is what my book says:
    Code:
    for miles in range(10, 70, 10):
           km = miles * 1.609
           print "%d miles --> %3.2f kilometers" % (miles, km)
    What my textbook says what the result has to be:
    Code:
    10 miles --> 16.09  kilometers
    20 miles --> 32.18  kilometers
    30 miles --> 48.27  kilometers
    40 miles --> 64.36  kilometers
    50 miles --> 80.45  kilometers
    60 miles --> 96.54  kilometer
    This is what I get:
    Code:
    >>> for miles in range (10, 70, 10):
    	km = miles * 1.609
    	print ("%d miles --> %3.2f kilometers") % (miles, km)
    
    	
    %d miles --> %3.2f kilometers
    Traceback (most recent call last):
      File "<pyshell#66>", line 3, in <module>
        print ("%d miles --> %3.2f kilometers") % (miles, km)
    TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
    >>> print km
    SyntaxError: invalid syntax
    I put ("%d miles --> %3.2f kilometers") between () because otherwise I get SyntaxError: invalid syntax

    Could someone explain this to me, what I do rong and what I should do.
    I use IDLE on Mac OS X.
    Thank you very much

    Greetings,
    Rhaiko
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    Originally Posted by Rhaiko
    This is what I get:
    Code:
    >>> for miles in range (10, 70, 10):
    	km = miles * 1.609
    	print ("%d miles --> %3.2f kilometers") % (miles, km)
    
    	
    %d miles --> %3.2f kilometers
    Traceback (most recent call last):
      File "<pyshell#66>", line 3, in <module>
        print ("%d miles --> %3.2f kilometers") % (miles, km)
    TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
    >>> print km
    SyntaxError: invalid syntax
    The book is for Python 2 and you seem to be running Python 3. Easy fix:

    Code:
        print("%d miles --> %3.2f kilometers" % (miles, km))
    (What’s important are the parentheses, not the removal of a space between “print” and the opening parenthesis, but it’s better style this way.)
    My armada: openSUSE 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    You are using python3 whereas this book you've chosen was written for python2. Are you a dummy? I doubt it. I wouldn't choose that book, and neither should you.

    In python3 print is a function whereas it was a statement. python functions and methods always return a value. If you don't choose a value, None is the returned on your behalf. Those who designed the python print function didn't select a return value (number of characters printed could have been a choice), and so returns None. Hang on, we're getting to the explanations.

    Given that print is a function explains your comment that
    I put ("%d miles --> %3.2f kilometers") between () because otherwise I get SyntaxError: invalid syntax
    In python you can assign functions

    a=print

    You can pass them as arguments:

    myfunc(print)

    They can stand alone as (useless, but valid) expressions:

    print
    divmod
    [edit]leading to the exceedingly useful use as a data object in a list or dictionary, for example.[/edit]

    Or you can call them:

    print(divmod(11,3))


    Leaving for syntax error:

    print 'xyz'



    Now, what was wrong with
    print("%d miles --> %3.2f kilometers") % (miles, km)
    ?
    python evaluated
    print("%d miles --> %3.2f kilometers")
    which returned None
    Then tried to evaluate

    None % (miles, km)

    Giving you the first error you report.

    How do you fix it? You could process the programs in the book with the 2to3 program. You could install python2.7 and use that version. Or correctly parenthesize:
    Code:
    for miles in range(10, 70, 10):
           km = miles * 1.609
           print ("%d miles --> %3.2f kilometers" % (miles, km))

    Comments on this post

    • rrashkin agrees : thoughtful, helpful, and didactic.
    Last edited by b49P23TIvg; March 13th, 2013 at 01:50 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0
    Thanks

    Is it better in your opinion to buy a new book for Python 3. Or aren't there so many differents?

    And what books would you guys reccoment?

    I found the book Think Python.
    Do you guys think this book will give a good basis?
    Last edited by Rhaiko; March 13th, 2013 at 02:01 PM. Reason: wanted to give more information

IMN logo majestic logo threadwatch logo seochat tools logo