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 ...
- 12-26-2010 #1Just 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?
- 12-26-2010 #2
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)
- 12-26-2010 #3Just 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?
- 12-26-2010 #4
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 '
- 12-26-2010 #5
i figured it out, i loaded up python 3
should beCode:('Total Expenses = R$ %s.') % total
Code:('Total Expenses = R$ %s.' % total)
- 12-26-2010 #6Just Joined!
- Join Date
- Dec 2010
- Posts
- 5
now it's allright! Thanks coopstah!


Reply With Quote