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

Hey there. I'm having trouble with problem set 1, on the very first problem. I can get IDLE to accept my raw inputs, but it doesn't let me do any math with the results. Like say I name the outstanding balance 'b' (without quotations), it tells me I can't do math with b, even though I thought I defined it as a number. I also may not be using the right terminology, being totally new to programming.

OpenStudy (anonymous):

Erm. Sorry about double posting, but. Does it have anything to do with the raw_input being a string, and not actually a number?

OpenStudy (anonymous):

Yes. You have to parse a raw input into an integer.

OpenStudy (anonymous):

Thanks. I think just using float() around raw_input() is working. Wow, alright. I didn't know you could do that.

OpenStudy (turingtest):

I don't know what you're doing exactly, but data types (whether what you put in is a string or number), are not the issue. raw_input() can take most elementary data typed: ints, floats, strings, lists, etc. Try something like this in IDLE: >>> b=raw_input("word ") word this >>> b+" is" 'this is' >>> n=raw_input("number: ") number: 5 >>> n '5' *this* and *5* are input when prompted mind you that '5' has been turned into a string now by raw_input(), so trying something like n+4 will throw an error unless you turn n into a number first (change its data type) Also, the terminology here is that the thing on the right of the = is *assigned to* the variable on the left. In the line n=5. I think that is the word you were looking for when you said "defined it as a number", here the jargon is "assigned the value 5 to n"

OpenStudy (e.mccormick):

``` Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> n=raw_input("number: ") number: 5 >>> 5 + n Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> 5 + n TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> n=int(raw_input("number: ")) number: 5 >>> 5+n 10 >>> ```

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!