我的基本文字要求进行定义,但我不知道为什么。
我尝试将其从列表更改为字符串。
我的代码是:
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)
错误代码:
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
我的例外结果是,它告诉我这是回文症,但它会吐出一条信息,说需要定义“ owo”。
答案 0 :(得分:0)
原始代码中有很多错误。试试:
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}")
在您的原始代码中,text_list_reversed = text_list.reverse()
失败,因为reverse()
在原地反转,并返回None。另外,text
是一个未初始化的变量,它表示您可能没有复制粘贴代码,而是重新输入了代码(如果您没有为此错误)。