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!
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
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?
Hi, No loops allowed ? then try:
x = '1234321'
y = x[::-1]
Is y == x ?
Paul
(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
(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.
(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]?
(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