Python Forum
Program to check whether a number is palindrome or not
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program to check whether a number is palindrome or not
#1
Bug 
A palindrome number is a number which if written backwards also, the number will look the same
Eg: 112211, 121, 898

I have a homework to write a program to check if a number is palindrome or not using concepts of loops
I failed miserably.

My code:
Input = input("Enter any number: ")
LengthOfInput = int(len(Input))
for x in range(LengthOfInput):
    a = 10


while Input:
    if Input[0] == Input[LengthOfInput]:
        continue
        if Input[x] == Input[LengthOfInput-x]:
            x=10
    else:
        print("Number is not palindrome")

print("The number is a palindrome")
  
Error:
LengthOfInput = len(Input) TypeError: object of type 'int' has no len()
Please help with this project, it is a very interesting project that I was not able to figure out by myself

Thank you!
Reply
#2
There are servals problems here,and you can only get that error message if using Python 2.7.
So Python 2 is dead💀 for many years ago,so you need to figure that out.
Here some code to help.
# pali.py
number = input("Enter any number: ")
lengt_of_input = int(len(number))
is_palindrome = True
for n in range(lengt_of_input // 2):
    if number[n] != number[lengt_of_input- n - 1]:
        is_palindrome = False
        break
Test.
# Python version,so 3.11 is newest one  
G:\div_code
λ python -V
Python 3.11.3

G:\div_code
λ ptpython -i pali.py
Enter any number: 898
>>> is_palindrome
True

G:\div_code
λ ptpython -i pali.py
Enter any number: 12341
>>> is_palindrome
False
Reply
#3
Thanks very much for the help!

So are you telling me that if I download Python 3.11(newest version) then this program will work?, because I think in Python 2.7 value of len() cannot be an integer and in Python 3 value of len() can be an integer?
Reply
#4
Hi, No loops allowed ? then try:
x = '1234321'
y = x[::-1]
Is y == x ?
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
(Sep-07-2023, 01:33 PM)DPaul Wrote: Hi, No loops allowed ? then try:
x = '1234321'
y = x[::-1]
Is y == x ?
Paul

Thanks for the reply but the homework is to do it using loops
Reply
#6
(Sep-07-2023, 01:23 PM)PythonBoy Wrote: So are you telling me that if I download Python 3.11(newest version) then this program will work?, because I think in Python 2.7 value of len() cannot be an integer and in Python 3 value of len() can be an integer?
The problem is line before input("Enter any number: ") in Python 2.7 this will return a integer,and in Python 3(input() change) it will return a string.
Yes it will work with Python 3.11.
Reply
#7
(Sep-07-2023, 01:33 PM)DPaul Wrote: Hi, No loops allowed ? then try:
x = '1234321'
y = x[::-1]
Is y == x ?
Paul

Although this is not the want, my teacher wants me to do it,

I am interested in this method also. Could you please explain why did we write y = x[::-1]?
Reply
#8
(Sep-07-2023, 01:38 PM)snippsat Wrote:
(Sep-07-2023, 01:23 PM)PythonBoy Wrote: So are you telling me that if I download Python 3.11(newest version) then this program will work?, because I think in Python 2.7 value of len() cannot be an integer and in Python 3 value of len() can be an integer?
The problem is line before input("Enter any number: ") in Python 2.7 this will return a integer,and in Python 3(input() change) it will return a string.
Yes it will work with Python 3.11.

Oh Thank you very much for the good news, I will download the newest version of Python and try it out
Reply
#9
(Sep-07-2023, 01:42 PM)PythonBoy Wrote:
(Sep-07-2023, 01:33 PM)DPaul Wrote: Hi, No loops allowed ? then try:
x = '1234321'
y = x[::-1]
Is y == x ?
Paul

Although this is not the way my teacher wants me to do it,

I am interested in this method also. Could you please explain why did we write y = x[::-1]?
Reply
#10
(Sep-07-2023, 01:43 PM)PythonBoy Wrote: I am interested in this method also. Could you please explain why did we write y = x[::-1]?

Look here:
https://www.guru99.com/1-in-python.html
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Program to Find the Factorial of a Number elisahill 2 1,442 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,635 Feb-24-2022, 10:40 AM
Last Post: perfringo
  Palindrome checker case sensive edwdas 3 2,651 Nov-07-2019, 05:57 PM
Last Post: nilamo
  Program that displays the number with the greatest amount of factors ilusmd 3 2,826 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Palindrome program - I can't figure it out. Gabar112 3 3,543 Feb-20-2018, 07:03 PM
Last Post: Larz60+
  Palindrome.strip pirts.emordnilaP RodNintendeaux 4 3,868 Oct-08-2017, 02:30 AM
Last Post: RodNintendeaux
  Program to print: Last Name, ID, Mobile Number, All panick1992 14 9,825 Mar-15-2017, 02:46 PM
Last Post: panick1992

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020