Here's the question you clicked on:
Gen.Gluck
I have to create a function that prints the palindromes between two numbers, this is what I have so far: def isPalindrome(n,m): for i in range(n,m): i=0 dig=i%10 rev=rev*10+dig i=i/10 print(i) can anyone assist?
what is this for? c++, c, java?
It's Python and the above code does this. It just sets i to 0, gets the modulus of 0/10 so dig is 0, then takes rev and multiplies it by ten and adds 0 to it which will create an error if rev wasn't defined elsewhere. let's pretend it was set to 0 elsewhere, so 0*10+0 is 0. Then you divide i by 10, since i was never changed it's still 0 so we make it 0 again. It repeats this process a number of times equal to the difference of m and n. And then we print 0 (well, actually we don't because based on the indentation it is outside the function and therefore doesn't know what i is so just gives us an error). Not sure what you thought you were doing here nor do I understand what you mean by "prints the palindromes between two numbers". Normally something like 'isPalindrome' would simply return true if the item is a palindrome and false if it is not. But then the function only needs 1 argument and not two. With two arguments maybe you are trying to see if one is the reverse of the other, hard to say. You will need to explain what you mean a little better and provide sample input/output.
This is what the program is supposed to look like: Enter the starting point N: 200 Enter the ending point M: 800 The palindrome numbers are: 313 353 373 383 727 757 787 797
Something like this should work def isPalindrome(m,n): for i in range(m,n): if str(i)==str(i)[::-1]: print i
This worked: def isPalindrome(n,m): for i in range(n,m): if str(i)==str(i)[::-1]: print(i) n= eval(input("Enter the starting point N:\n")) m=eval(input("Enter the ending point M:\n")) print(isPalindrome(n,m)) Thank you
Does it work with 4 digit numbers?
Seems to >>> isPalindrome(1000,10000) 1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 2002 2112 2222 2332 2442 2552 2662 2772 2882 2992 3003 3113 3223 3333 3443 3553 3663 3773 3883 3993 4004 4114 4224 4334 4444 4554 4664 4774 4884 4994 5005 5115 5225 5335 5445 5555 5665 5775 5885 5995 6006 6116 6226 6336 6446 6556 6666 6776 6886 6996 7007 7117 7227 7337 7447 7557 7667 7777 7887 7997 8008 8118 8228 8338 8448 8558 8668 8778 8888 8998 9009 9119 9229 9339 9449 9559 9669 9779 9889 9999