0

My base text is asking to be defined but I can't work out why.

I've tried changing it from list to string.

My code is:

text_str = input("What is your text: ")

text_list = list(text)

text_list_reversed = text_list.reverse()

text_str_reversed = str(text_list_reversed)

if text_str_reversed == text_str:
    print("Your word is a palindrome")

else:
    print("Your original was",text_str)
    print("Your word reversed is",text_str_reversed)

Error code:

What is your text: owo

Traceback (most recent call last):
  File "C:/Users/sambi.LAPTOP-UU5B18TJ/OneDrive/Documents/Python/My Code/School Stuff/Palindrome checker.py", line 1, in <module>
    text_str = input("What is your text: ")
  File "<string>", line 1, in <module>
NameError: name 'owo' is not defined

My excepted result would be for it to tell me it's a palindrome but instead it's spitting out a message saying that "owo" needs to be defined.

pault
  • 41,343
  • 15
  • 107
  • 149
Syphx
  • 3
  • 2
  • Just as a heads up, your `print`s suggest that you're intending to use a Python 3 interpreter, but you're actually using a Python 2 interpreter. You wouldn't get this error if you were running this as Python 3 code. – Carcigenicate Jun 19 '19 at 20:11
  • Also, you are saving input to `text_str` and then using `text_list = list(text)`. Where did `text` come from? – Mark Jun 19 '19 at 20:12

1 Answers1

0

Lot going wrong in the original code. Try:

def pal_check():
    text_str = input("What is your text: ")
    text_str_rev = text_str[::-1]
    if text_str_rev == text_str:
        print("Your word is a palindrome")
    else:
        print(f"Your original was: {text_str}")
        print(f"Your word reversed is: {text_str_rev}")

In your original code, text_list_reversed = text_list.reverse() fails because reverse() reverses in place, and returns None. Also, text is an uninitialized variable, which indicates you probably didn't copy-paste the code, but rather retyped it, if you weren't getting an error for that.

Mike
  • 828
  • 8
  • 21