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

What is wrong with my python code? Mooc 6.189 user_input = int(raw_input('enter integer here: ')) print ' you entered the value', user_input while abs(user_input) > 0: abs(user_input) = abs(user_input) - 1

OpenStudy (anonymous):

You cannot attribute value to an expression... ie : abs(user_input) is invalid. If you want to take care of negative values, you should probably do something like user_input = abs(user_input) before the loop...

OpenStudy (anonymous):

btw, you could just modify abs(user_input) = abs(user_input) - 1 by user_input = abs(use_input) - 1 but after the first iteration, all abs() operation would be useless, as the number would have been converted to positive at first loop...

OpenStudy (anonymous):

So this is a better code? user_input = int(raw_input('enter integer here: ')) print ' you entered the value', user_input while abs(user_input) > 0: if user_input < 0: user_input = user_input + 1 print user_input else: user_input = user_input - 1 print user_input

OpenStudy (anonymous):

Sure, much better like this !

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!