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

what is wrong with my code to test if a number is odd or even x=raw_input("enter a number:") print x if (x%2)==0: print "even" else: print "odd"

OpenStudy (anonymous):

i also tried replacing the print x line with int(x) to make sure the x being plugged in was an integer?

OpenStudy (anonymous):

You would have to specify that the raw_input data is an int. Otherwise it looks as though it returns x as a string. Use: x = int(raw_input("Enter a number"))

OpenStudy (anonymous):

ok that makes sense but if i used the function int(x) on the next line i don't see why it wouldn't return the same answer

OpenStudy (anonymous):

Ya it should work.

OpenStudy (anonymous):

Oh no...x remains a string because it cannot be changed that way.

OpenStudy (anonymous):

O Okay Thanks

OpenStudy (anonymous):

Ex. x = 8 vs x = "8" x = 8 : x*2 = 16 x = "8" : x*2 = "88"

OpenStudy (anonymous):

Okay Thanks

OpenStudy (anonymous):

Onyx, when you said you replaced 'print x' with 'int(x)' is that all that was on that line? To make sure x is an int, you either need to wrap the raw_input in an int() or do x = int(x) afterwards. int(x) just returns x converted to an int, it doesn't alter x on its own.

OpenStudy (anonymous):

Augh, I wish we could edit or delete posts.

OpenStudy (anonymous):

haha - I said the same thing...or at least be able to edit them for the first 5 minutes or so!

OpenStudy (anonymous):

Yeah that would be cool. Keen thanks i forgot about how functions don't actually change it, it only ouputs it. That would be very important haha. O and sorry about late response I have school so I'm not on much during the week.

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!