Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

I was just playing in IDLE and wrote the following: >>> sum = 0.0 >>> for i in range(10): sum = sum + 0.1 frustratingly it was giving me traceback errors saying in line 2, + is not a valid action or so. then i reopened the IDLE and wrote the same program. Sure enough it was working just fine this time as it should be. Any idea why the error was happening the first time around?

OpenStudy (e.mccormick):

Hmmm.... Did you perhaps type sum = "0.0" the first time?

OpenStudy (anonymous):

hey thanks! yes i did write that the firt time..and the second time too..

OpenStudy (e.mccormick):

Becaus if you use quotes, that makes it text, not a number.

OpenStudy (anonymous):

ok u mean with the quotations..no i dont think so..let me check

OpenStudy (turingtest):

sum is a built-in operation, that should be a problem

OpenStudy (e.mccormick):

See, like this: ``` >>> sum = "0.0" >>> for i in range(10): sum = sum + 0.1 Traceback (most recent call last): File "<pyshell#3>", line 2, in <module> sum = sum + 0.1 TypeError: cannot concatenate 'str' and 'float' objects >>> sum = 0.0 >>> for i in range(10): sum = sum + 0.1 >>> print sum 1.0 >>> ```

OpenStudy (anonymous):

no i didnt use quotations..:(

OpenStudy (anonymous):

@Turing test, if it was how come it worked later?

OpenStudy (e.mccormick):

Turing, it is a function, not an operation, but yes, always best to avoid those.

OpenStudy (turingtest):

no it works, i checked. my mistake

OpenStudy (turingtest):

it's not an indentation issue?

OpenStudy (anonymous):

no i dont think so...i should have copied the traceback error though...to make things clearer..

OpenStudy (e.mccormick):

Yah, without the traceback it is hard to say. Perhaps a small typo. Then it would be undefined.

OpenStudy (e.mccormick):

For example: ``` >>> ================================ RESTART ================================ >>> sim = 0.2 >>> for i in range(10): sum = sum + 0.1 Traceback (most recent call last): File "<pyshell#12>", line 2, in <module> sum = sum + 0.1 TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'float' ``` Which is why it is best to use var names that are not function names.

OpenStudy (anonymous):

yeah perhaps so..thing is it is so silly it was driving me mad..also more later when the code actually worked..anyways..perhaps it was nothing...thanks a lot u both..really appreciate it..:)

OpenStudy (e.mccormick):

np. Have fun!

OpenStudy (turingtest):

or sometimes just restarting IDLE fixes it, too :P

OpenStudy (anonymous):

" unsupported operand type(s) for +"! YES! that's what it told me!

OpenStudy (anonymous):

haa...haa...u got that right @Turing! :D

OpenStudy (turingtest):

:)

OpenStudy (e.mccormick):

Yah, if it said unsuppored operand it was some sort of type mismatch, which a typo can do. And like Turing pointed out, it is just best to avoid the built-ins as var names. These are them: https://docs.python.org/2/library/functions.html

OpenStudy (anonymous):

thanks @e.mccormick! i'll watch out next time! :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!