In script of reverse a number, I saw that every time i divide the number by 10, i need to use type conversion, why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In script of reverse a number, I saw that every time i divide the number by 10, i need to use type conversion, why?

while True: print("\tReverse A Number\n") num = int(input("Enter a Number: ")) new_num = 0 print("\n") while num != 0: rem = num%10 num = int(num/10) new_num = new_num*10 + rem print("The Number After Reversal " + str(new_num)) In the second while block When i replace num = int(num/10) by num = num/10 it gives output as The Number After Reversal inf what is inf? and why this happened?

20th Jan 2017, 5:59 AM
Gaurav
Gaurav - avatar
7 Answers
+ 5
Because division in python returns FLOAT You can use floor division // . It will return integer when you will divide two integers
20th Jan 2017, 6:25 AM
WittyBit
WittyBit - avatar
+ 5
inf is infinity
20th Jan 2017, 7:12 AM
WittyBit
WittyBit - avatar
+ 4
So when you divide by 10 it is float => infinite loop
20th Jan 2017, 7:12 AM
WittyBit
WittyBit - avatar
+ 3
@Gaurav you mean int? If yes - it coverts float(real) number to int(only full part) also can converts strings to int
20th Jan 2017, 6:31 AM
WittyBit
WittyBit - avatar
0
and what is inf stands for?
20th Jan 2017, 6:29 AM
Gaurav
Gaurav - avatar
0
nope I mean inf , when you use int = int/ 10 the output you got :The Number After Reversal inf
20th Jan 2017, 6:34 AM
Gaurav
Gaurav - avatar
0
OK thanks
20th Jan 2017, 7:27 AM
Gaurav
Gaurav - avatar