Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, Yesterday I was in doubt about which language to learn, and then i asked here, someone told me to learn Python. So I started researching and found that to ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    5

    Trouble with Python 3

    Hello,

    Yesterday I was in doubt about which language to learn, and then i asked here, someone told me to learn Python.

    So I started researching and found that to confuse me even more there is the python 2 and 3, but I thought "should not be much difference, I will learn the 3 because it is more complete"

    At first everything was fine, I was following a tutorial for python 2, but until then was completely applicable in the python 3, except that the command "print" needs to be put in ( ), but until i could figure out that difference alone.

    But now I encounter a problem, running this script:

    # test.py - Calculator of expenses

    print ("Balance of domestic expenses")
    ana = input ('How many Ana spent?')
    bia = input ('How many bia spent?')
    total = float (ana) + float (bia)
    print ('Total Expenses = R$ %s.') % total
    average = total/2
    print ('Expenses per person = R$ %s.') % average

    When i run it on python 2 it runs fine, but on 3 i receive this error:

    Balance of domestic expenses
    How many Ana spent?3
    How many bia spent?6
    Total Expenses = R$ %s.
    Traceback (most recent call last):
    File "/home/eddie/Documentos/test.py", line 7, in <module>
    print ('Total Expenses = R$ %s.') % total
    TypeError: unsupported operand type(s) for %: 'NoneType' and 'float'

    It seems to me the "%"is read differently in python 2 and 3 as in the "print", but I can not figure out a way around this, can someone help me?

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    perhaps python2 allows incorrect syntax for string formatting

    %s is for string, you would want to use %f for float variable in string formatting

    python 3 isn't "more complete", it is a new version of the language with newer and different features, python 2.x is more stable as it has been around a long time and is suitable for most people (also not all 3rd party works on 3)

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    5
    Traceback (most recent call last):
    File "/home/eddie/Documentos/test3.py", line 6, in <module>
    print ('Avegare Expenses = R$ %f.') % total
    TypeError: unsupported operand type(s) for %: 'NoneType' and 'float'

    %f. doesn't work neighter...

    So i guess it's better to begin with python 2 instead of 3?

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    so, apparently for some reason total is interpreted as being null, i'm not sure why this would happen based on what your code is

    unless ' changes meaning in python3, i was pretty sure in python2 you could use them for strings, but i never use them, always use "

    try changing your print to use " instead of '

  5. #5
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    i figured it out, i loaded up python 3

    Code:
    ('Total Expenses = R$ %s.') % total
    should be
    Code:
    ('Total Expenses = R$ %s.' % total)

  6. #6
    Just Joined!
    Join Date
    Dec 2010
    Posts
    5
    now it's allright! Thanks coopstah!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...