为什么在使用反向操作时为我的列表分配了 "none" 值?



我是python的新手,我正在尝试创建一个程序,告诉用户他们输入的单词是否是回文。当我执行代码时,它会输出以下内容:

请输入一个单词。我会告诉你这个词是否是回文:hannah

请输入一个单词。我会告诉你这个词是不是回文:hannah这个词不是回文

['n','a','h']

进程已完成,退出代码为0

我不确定cal_tableRev中的列表为什么保存为"none"。任何关于我如何解决这个问题的想法都将是一个很大的帮助!

user_input = input("Please enter a word. I will tell you if that word is a palindrome or not: ").lower()
cal_table1 = []
cal_table2 = []

for letter in user_input:
cal_table1.append(letter)
inputSize = len(cal_table1)
Calsize = inputSize / 2
if inputSize % 2 != 0:
print("The word has an odd number of letters and, therefore, it is not a palindrome. Please enter a new word")
for letters in cal_table1[0:int(Calsize)]:
cal_table2.append(letters)
cal_tableRev = str(cal_table2.reverse())
frontHalf = str(cal_tableRev)
backHalf = str(cal_table2)
calulated_word = str(frontHalf) + str(backHalf)
if user_input == calulated_word:
print("This word is a palindrome")
else:
print("This word is not a palindrome")
print(calulated_word)

函数reverse((反转给定列表,但返回值None,然后将其分配给cal_tableRev尝试:

cal_tableRev = copy.deepcopy(cal_table2)
cal_tableRev.reverse() #reversing without assigning the None value
cal_tableRev=str(cal_tableRev)

看起来你正在做很多python可以让你更轻松的工作。看看我在python控制台中运行的这些命令:

>>> word='tenet'
>>> backwards=''.join(reversed(word))
>>> word == backwards
True
>>> word='pizza'
>>> backwards=''.join(reversed(word))
>>> word == backwards
False
>>> word
'pizza'
>>> backwards
'azzip'

最新更新