def check_palindrome(word):
if len(word) == 0 or len(word) == 1:
return False
else:
begin = 0
end = -1
while begin != len(word)/2:
begin += 1
end -= 1
if word[begin] != word[end]:
return False
return True
word = "kayak"
if check_palindrome(word):
print("The word " + word + " matched!")
else:
print("The word " + word + " didn't matched!")
#๐ Checking palindrome word: index out of range
63 messages ยท Page 1 of 1 (latest)
@muted juniper
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
Where the code says:
while begin != len(word)/2:
What do you expect the value of len(word)/2 to be, for the input word being "kayak"? Now, verify that
Can begin ever be equal to that value?
ohhhhh
I see
I mean
if begin index reached the middle of the letter
it would be success
so how would it be turn out?
!e print(len("kayak") / 2)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
2.5
so it must be a floor division
right
(use // for this.)
I think "" and "x" (for example) should be counted as palindromes, yes?
there are maybe some more elegant ways to write it, but I think the issue is resolved and you learned the main point of the exercise
like aa -> correct. ch - > incorrect
wydm?
I appreciated your help!
I remember I did this with recursion and successed
is there any thoughts about this code?
nothing worth talking about at this stage, I think
bro
this code
def check_palindrome(word):
if len(word) == 0 or len(word) == 1:
return True
else:
begin = 0
end = -1
while begin != len(word)//2:
begin += 1
end -= 1
if word[begin] != word[end]:
return False
return True
word = "abc"
if check_palindrome(word):
print("The word " + word + " matched!")
else:
print("The word " + word + " didn't matched!")
shows the abc word matched
I edited this one.
consider when the letters are checked, vs. when begin and end are updated.
it skipped the if letters are not equal thats why
it didn't check the first and last letters against each other.
because begin and end change first, and then the comparison is tried.
oh ok
I changed it now:
if word[begin] != word[end]:
return False
else:
begin += 1
end -= 1
so it first must check if the letters are not equal
yes.
(We don't actually need else, because return means the rest of the code won't be reached anyway.)
Thanks for your help!
btw I'm certified in python
I came back to it
cuz I forgot and studied C++
just getting a review.
welcome back. I never really cared about any certifications, it's a big world out there and people have all kinds of opinions and standards
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.