+ 1

can some one help me out what is wrong whith this code?

def pal(n): rev=0 temp=n while temp>=1: div=temp%10 rev=(rev*10)+div temp=temp/10 print(rev) if rev==n: print("pal") else: print("not pal") print(pal(121)) program for palindrome

13th Apr 2017, 2:07 PM
Champs
4 ответов
+ 3
I'm not sure of the best way to do it with mathematical operations, but you could make it work easily by turning your integer into a string, reversing the string, and comparing the two: def pal(n): n = str(n) rev = n[::-1] if n == rev: print("pal") else: print("not pal") pal(121)
13th Apr 2017, 2:47 PM
Jeremy Warren
Jeremy Warren - avatar
+ 3
It's because Python automatically converts int types to float for division. If you want integer floor division like other languages do by default then you have to specify it in Python. Use: temp=temp//10
13th Apr 2017, 3:34 PM
Jeremy Warren
Jeremy Warren - avatar
0
Can you post the output please?
13th Apr 2017, 2:44 PM
Talha Waheed
Talha Waheed - avatar
0
the output i'm getting is 122.26 the actual output should be 121 yes, for strings we can use reverse() in java this code is working but not in python anyone can give the detail explanation?
13th Apr 2017, 3:14 PM
Champs