Ononto
Ononto

Reputation: 5

Generating a palindrome number taken from input

I'm writing a program that takes a number from input and generates it's palindrome number. My program only prints the first half not the second. I tried reverse, didnt work.I have incluede those lines as comment.

My Code:

def show_palindrome(maximum):
    maximum = int(input("Enter Number : "))
    for number in range(1, maximum + 1):
        temporary = number
        reverse = 0
    
        while (temporary > 0):
            reminder = temporary % 10
            reverse = (reverse * 10) + reminder
            temporary = temporary //10

        if(number == reverse):
            #number2 = number[::-1]
            #print(number,number2, end = '')
            print(number, end = '')

show_palindrome(3)

My output:

123

The output I need:

12321

Upvotes: 0

Views: 601

Answers (5)

azro
azro

Reputation: 54168

Your code seems over complicated, and I don't even know what to fix in it. What I can tell is that passing a parameter maximum to overwrite it just after with maximum = int(input("Enter Number : ")) is useless, don't pass the parameter.


So let's back to easy things

  1. Build palindrome using strings method : slicing backwards from index -2, and reverse it with -1 increment

    def show_palindrome():
        value = input("Enter Number : ")
        print(value + value[-2::-1])
    
  2. Build palindrome using math operations : save the remainder, except the first one

    def show_palindrome():
        value = input("Enter Number : ")
        result = value
    
        value = int(value) // 10  # remove last char which would be redundant
        while value > 0:
            result += str(value % 10)
            value = value // 10
    
        print(result)
    
    show_palindrome()
    

Upvotes: 1

Roman
Roman

Reputation: 451

def palindrome(maximum):
    number = ''.join([str(num) for num in range(1, maximum + 1)])
    return int(number + number[-2::-1])

print(palindrome(3))

Output:

12321

Upvotes: 1

heretolearn
heretolearn

Reputation: 6555

Apart from the string slice method (as used by @PApostol), you could also use the reversed method :

def show_palindrome():
    value = input("Enter Number : ")
    print(value[:-1] + "".join(reversed(value))) 

Input:

123

Output:

12321

Upvotes: 1

Eric Taurone
Eric Taurone

Reputation: 137

I believe you're looking for something like this:

def show_palindrome(maximum = None):
    if not maximum:
        maximum = input("Enter Number : ")
        
    output = str(maximum)
    for number in range(1, int(maximum)):

        output = str(int(maximum) - number) + str(output) + str(int(maximum) - number)

        
    return output

print(show_palindrome(3))

this returns 12321 for instance

A couple of things I would do differently in your function:

  1. If you're going to require the input in the def (The way you make it optional is to set it equal to something when you declare it like I have it set to None (maximum=None), then you don't need the input() statement.
  2. Since you already know how long you want it to be ( you require it declared when you initialize the function, it's just 2*maximum - 1) there's really no need to use a while loop.

Other than that good job! Keep it up!

Upvotes: 1

PApostol
PApostol

Reputation: 2292

You can try something simpler like this:

def show_palindrome():
    num = input("Enter Number : ")
    print(num + num[:-1][::-1])
        
show_palindrome()

Input:

123
12
1

Output:

12321
121
1

Upvotes: 1

Related Questions