Python Forum
Reverse order of phrase Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reverse order of phrase Error
#1
phrase = input("Enter a phrase")
rev_phrase = reversed(phrase)
i = len(phrase)
while i >= 0:
    if phrase[i] != rev_phrase[i]
        print(phrase + " is not a palindrome")
        exit()
    i -= 1
print(phrase + "is a palindrome")
Trying to reverse the order of a phrase, then check if it spells out the same thing before reversing. However, I am getting an error in line 5 where I am reversing the phrase.

My Error Message:

C:\Users\Mikhail\PycharmProjects\FirstUse\venv\Scripts\python.exe "C:/Users/Mikhail/PycharmProjects/FirstUse/venv/Exersise Six.py"
File "C:/Users/Mikhail/PycharmProjects/FirstUse/venv/Exersise Six.py", line 5
if phrase[i] != rev_phrase[i]
^
SyntaxError: invalid syntax

Process finished with exit code 1
Reply
#2
You need to have : at the end of line #5.

EDIT:

You should keep in mind that 'Anna' is considered palindrome as well as 'Step on no pets', 'Was it a car or a cat I saw?'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
phrase = input("Enter a phrase")
rev_phrase = reversed(phrase)
i = len(phrase)
while i >= 0:
    if phrase[i] != rev_phrase[i]:
        print(phrase + " is not a palindrome")
        exit()
    i -= 1
print(phrase + "is a palindrome")
Now I am getting this error in #line 5:

C:\Users\Mikhail\PycharmProjects\FirstUse\venv\Scripts\python.exe "C:/Users/Mikhail/PycharmProjects/FirstUse/venv/Exersise Six.py"
Enter a phrasepop
Traceback (most recent call last):
File "C:/Users/Mikhail/PycharmProjects/FirstUse/venv/Exersise Six.py", line 5, in <module>
if phrase[i] != rev_phrase[i]:
IndexError: string index out of range

Process finished with exit code 1
Reply
#4
Python uses zero based indexing therefore:

>>> a = 'abc'
>>> len(a)
3
>>> a[3]
/../
IndexError: string index out of range
>>> [a.index(char) for char in a]
[0, 1, 2]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
in python indexes are zero-based, thus max index in len(phrase)-1

now, unless you are required (e.g. homework) to use while loop and indexes, there are better , more pythonic ways to complete the task

using slices
if phrase == phrase[::-1]
    print("it's palindrome")
else:
    print("not palindrome") 
or even as oneliner
print("it's palindrome" if phrase == phrase[::-1] else "not palindrome")
if you want to compare char by char
for c1, c2 in zip(phrase, phrase[::-1]):
    if c1 != c2:
        print("not palindrome")
        break
else:
    print('palindrome')
if all(c1 == c2 for c1, c2 in zip(phrase, phrase[::-1])):
    print("palindrome")
else:
    print('not palindrome')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get a reverse order of a number? tonyflake 1 1,504 Apr-16-2020, 01:42 PM
Last Post: deanhystad
  length constraint on phrase hash to password javaben 0 1,919 Aug-21-2019, 05:34 PM
Last Post: javaben
  Finding exact phrase in list graham23s 2 2,900 Mar-13-2019, 06:47 PM
Last Post: graham23s
  Probelm using Boolean input phrase.isupper() skrivver99 4 3,112 Nov-04-2018, 09:21 AM
Last Post: Larz60+
  phrase loop and character comparision shiro26 6 4,082 Jul-06-2018, 02:03 AM
Last Post: shiro26
  Input a number with any number of digits and output it in reverse order of digits. sarada2099 6 6,666 Mar-12-2017, 05:45 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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