The Student Room Group

programming in python

i'm trying to write a program for identifying a palindrome in python and wondered if anyone could help? it keeps saying error

n= str(input("palindrome")


def is_palindrome(n):
return True
for in range (0, int(len[n])/2):
if n!= n[-i+1]:
return False
break


if is_palindrome = True:
print ("palindrome")


if is_palindrome = False:
print ("not palindrome")

Scroll to see replies

Sorry for not being helpful but can I ask why are you learning Python? I found it really painful and unintuitive to learn. Seems like a hobbyist language rather than a serious one.
Reply 2
everything in bold is wrong. in python, integer division is // not /. Your function always returns true, and bear in mind theres no need to cast the input to a string, its already a string
Python isn't my strong point, so bear with me here :lol:

However it looks like at least part of the issue is with the declaration of the for loop:


for in range (0, int(len[n])/2):
if n!= n[-i+1]:
return False
break


You need to specify it's for i in the range, which it doesn't currently :smile:

Also, as has been mentioned, your function currently always returns true.
(edited 9 years ago)
Reply 4
further to the above poster, in python your line int(len[n].... should be len(n) // 2

you dont need to cast the return variable of the length function to an int, as it only returns ints.
Reply 5
Original post by The_Last_Melon
Sorry for not being helpful but can I ask why are you learning Python? I found it really painful and unintuitive to learn. Seems like a hobbyist language rather than a serious one.



I have to for Decisions in maths, but i don't really see the point in it either!
Reply 6
Thank you to everyone who has commented :smile:
it returns an error at the 'def is_palindrome' part as well?
It would be more helpful if you could give the specific error? :smile:

At a quick glance though, you seem to be calling the function without passing in n as the parameter.
Reply 8
Original post by shadowdweller
It would be more helpful if you could give the specific error? :smile:

At a quick glance though, you seem to be calling the function without passing in n as the parameter.


it just says "invalid syntax"
Could you post the updated code, if you've changed the previously mentioned errors?
Original post by shadowdweller
Could you post the updated code, if you've changed the previously mentioned errors?


n=(input("palindrome")




def is_palindrome(n):
for in range (0, len(n)//2):
if n!= n[-i+1]:
return False
break
for i in range(......) Here, think of i as a counter, it starts at 0, and increments up to len(n)//2.

the statement n[-i + 1] wont work, as arrays are indexed from 0 onwards
Original post by tim_123
for i in range(......) Here, think of i as a counter, it starts at 0, and increments up to len(n)//2.

the statement n[-i + 1] wont work, as arrays are indexed from 0 onwards


I thought that if a list was inputted, by having -i, it would select the item that was 'i' items from the end?
in this case, say the length of the string is 10. Then i will be, 0,1,2,3,4. So -i+1 will be 1,0,-1,-2,-3. What you could do to check is just make a for loop that prints the value of i at each iteration
Original post by tim_123
in this case, say the length of the string is 10. Then i will be, 0,1,2,3,4. So -i+1 will be 1,0,-1,-2,-3. What you could do to check is just make a for loop that prints the value of i at each iteration


I just tried to, but the program stops at the 'def is_p'... bit as it says the syntax is incorrect (?)
And maybe it's -(i+1) since that would give 0,1,2,3 with -1,-2,-3,-4 which i think is right?
Let me see the function and the call to the function.

Ok, so imagine the string is a linear structure. [h,e,l,l,o] like this. We can see that h is at index 0, e is at index 1 and so on. So -1 doesn't exist (sort of), at least you can't access the memory that is there. A simple for loop to iterate over this string would be...

For I in range(0, len(n)-1):
Print(n)

Would print hello

But print(n[-i+1]) wouldn't work
ahhhhh how do i make it work?? :frown:

n=(input("palindrome")


def is_palindrome(n):
print i
for i in range (0, len(n)//2):
if n!= n[-(i+1)]:
return False
What you might want to do, bearing in mind the structure of a palindrome, is use two loops to iterate over each half of the palindrome, checking If the letters at the opposite indexes match. So take [r,a,c,e,c,a,r]. Index 0 matches index 6 (r), index 1 matches index 5.

You might not even need two loops, just think about the maths involved in finding where two matching letters are in relation to eachother
The print i statement is invalid. You need to put it inside the for loop
You are learning PYTHON in MATHS? What the **** is happening to the world?

Quick Reply

Latest