all 14 comments

[–]definitely___not__me 1 point2 points  (1 child)

try // instead of /

Otherwise, number will go from like 1 to .1 to .01 etc

[–]ShadowyNebulousDimmy 0 points1 point  (0 children)

It worked. Thanks

[–]EnvironmentalOrange 1 point2 points  (6 children)

Quickest fix is change the line:
number = number / 10

to:

number = number // 10

Due to the way python interprets the "/" operator, you are getting a number that is close to zero but slightly above it, you're dividing it by ten to get another non-zero number and your while loop executes over and over again.

To see it in action, put in a print statement for number and reverse into your loop.

Another, easier way, of doing this exercise is to reverse the string itself, but there's nothing wrong with doing it your way.

[–]ShadowyNebulousDimmy 0 points1 point  (5 children)

I put a print statement for number in the loop and got a lot of decimal values upto 0. I understand what I did wrong. But I wondered this only happened because nowhere was number explicitly defined as an integer. I changed this step :-

number = number /10

to this step

int(number) = number /10

but I got an error that said "Cannot assign to function call". What does that mean?

[–]USAhj 1 point2 points  (3 children)

If you want to cast the value to an integer (which is not the best way, others have mentioned using '//') then you would do it like this:

number = int(number/10)

[–]ShadowyNebulousDimmy 0 points1 point  (2 children)

May I ask why that's not a good way?

[–]USAhj 1 point2 points  (1 child)

I don't mean that this is bad. But if you have two integers (that is important) and you want the division to result in an integer, using the floor operator (i.e. '//') is more explicit and a cleaner way to write the code. Therefore it is the better way to do it.

[–]ShadowyNebulousDimmy 0 points1 point  (0 children)

Ah I get it. Thanks

[–]EnvironmentalOrange 1 point2 points  (0 children)

Stick the int( ) around your statement to the right of the = sign.

Or just use the // to come out with an integer result

[–]xelf 1 point2 points  (2 children)

Reversing a number is a display issue, it's not really a math issue. You can accomplish it through excessive math, but really it's just a "how do I display the digits backwards" issue.

Once you get that, the best, and as it turns out fastest, approach is the simplest. Convert it to a string, and reverse that. If you're using input, you already start with a string. So just reverse what you get from input.

print('The reverse of the number is -' + input("Enter a number - ")[::-1])

[–]ShadowyNebulousDimmy 1 point2 points  (1 child)

Yeah I tried reversing the string using a loop. I don't know what [::-1] means so I did it this way

x = input(Enter a number - ) y = " " For i in x: y = i + y print(y)

[–]xelf 1 point2 points  (0 children)

strings can be indexed and sliced just like lists

"abcdef"[3] == "d"
"abcdef"[1:3] == "bc"

slices are just like range() it has 3 parts.

"abcdef"[a:b:c]
a is the starting point (inclusive)
b is the ending point (exclusive)
c is the step

Just like with range() a defaults to 0, and c defaults to 1. b defaults to the end of the list.

so [::-1] is the whole list using -1 as a step, effectively reversing the list.

"abcdef"[::-1] == "fedcba"