Next Palindrome number
#1
Can you help me figure out what is causing an issue with my programming code as I am new to this?
If the number entered by the user (n) is not a palindrome, I want to print the next palindrome number.
I have taken the reference of this problem from this problem post.
Code:
n = int(input("Enter any number :- "))

reverse = 0
temp = n

while (n!=0):
    reverse = reverse * 10
    reverse = reverse + n%10
    n=n//10
if(temp==reverse):
    print ("Already palindrome:: ")

if(temp != reverse):
     new_temp = temp
     new_reverse = 0
     for i in range(new_temp,new_temp+10):
        while(temp != 0):
            new_reverse = new_reverse * 10
            new_reverse = new_reverse + temp%10
            temp = temp//10
         if(new_temp==new_reverse):
             print ("Next pallindrome is :- ",new_temp)
             break
         if(new_temp != new_reverse):
             temp = new_temp+1
Reply
#2
Line 21 and Line 25: Indentation error. That is easily fixed.

Now, try adding a print after line 17 (which is the while loop)
print the values of temp and new_reverse.

Input a 2 digit number; say 15. Watch the output. See if you can figure it out.

Hint: What does 'i' do.

There are other problems with the code. But see if you can solve this by yourself first. This will help in the long run.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Displaying number of comments in torrents with UserScripts NokTham 20 51,055 May 19, 2015, 18:36 pm
Last Post: NokTham



Users browsing this thread: 1 Guest(s)