回文、Python、问题

2024-10-01 09:37:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个新手python编码学习的乐趣。我想知道为什么这个程序很难输出正确的输出。我认为问题在于程序的结尾“if list==inverselist:”。 我总是得到一个输出,告诉我这个词是回文,即使它不是(例如老虎)

#Exercise 6 - Ask the user for a string and print out whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)

possiblepalindrome = str(input("Put in the possible palindromic statement here: "))
print(possiblepalindrome)
list=[]

for x in possiblepalindrome: #put each individual string character in a new list as its own element
    list.append(x)
print ('this is list', list)

for x in list: #Removes all spaces so multiple word/sentences can be palindrome
    if x is ' ':
        list.remove(' ')
print('this is with removed spaces' , list) 


def reverselist(argument): #This is a function. We put in some list, then the list is reversed, and is spat back out
    argument.reverse()
    return argument

inverselist = reverselist(list) #We use the reverselist function to make an inverse list of the palindrome
print('this is inverselist',inverselist)


if list == inverselist:
    print('Congratulations ', '"', possiblepalindrome, '"', ' is a palindrome!')
else:
    print('Unfortunately', '"', possiblepalindrome, '"', 'is not a palindrome.')

Tags: andtheinforstringifisthis
3条回答

你发布了太多的代码,而没有费心去测试它reverselist没有按您期望的方式工作。它反转输入列表,这是一个可变对象

def reverselist(argument): #This is a function. We put in some list, then the list is reversed, and is spat back out
    argument.reverse()
    return argument

word = list("Hello, world")
print(word)
inverseword = reverselist(word)
print(word, '\n', inverseword)

输出:

['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
['d', 'l', 'r', 'o', 'w', ' ', ',', 'o', 'l', 'l', 'e', 'H'] 
['d', 'l', 'r', 'o', 'w', ' ', ',', 'o', 'l', 'l', 'e', 'H']

简而言之,您颠倒了列表,然后将结果与列表本身进行比较

注:

  • 不要使用预定义的名称作为变量。我把list改为word
  • 查找如何反转序列。我们希望你在这里发布之前做适当的研究
  • 学习测试代码块。请参阅debugging help的这个可爱的参考

您的功能可以是:

return argument[::-1]

首先,在Python中为列表命名list很诱人,但很危险,因为您将无法调用list()构造函数

基本上你的代码可以这样简化

possiblepalindrome = str(input("Put in the possible palindromic statement here: "))
print(possiblepalindrome)

normal_list = list(possiblepalindrome.remove(" "))
inverselist= list(reversed(possiblepalindrome))
print('this is inverselist',"".join(inverselist))

if list == inverselist:
    print('Congratulations ', '"', possiblepalindrome, '"', ' is a palindrome!')
else:
    print('Unfortunately', '"', possiblepalindrome, '"', 'is not a palindrome.')

listinverselist有对同一列表的引用。您需要克隆list

只需更改reverselist函数

def reverselist(argument): #This is a function. We put in some list, then the list is reversed, and is spat back out
    li_copy = [] 
    li_copy.extend(argument)
    li_copy.reverse()
    return li_copy

完整代码:

possiblepalindrome = str(input("Put in the possible palindromic statement here: "))
print(possiblepalindrome)
list=[]

for x in possiblepalindrome: #put each individual string character in a new list as its own element
    list.append(x)
print ('this is list', list)

for x in list: #Removes all spaces so multiple word/sentences can be palindrome
    if x is ' ':
        list.remove(' ')
print('this is with removed spaces' , list) 


def reverselist(argument): #This is a function. We put in some list, then the list is reversed, and is spat back out
    li_copy = [] 
    li_copy.extend(argument)
    li_copy.reverse()
    return li_copy

inverselist = reverselist(list) #We use the reverselist function to make an inverse list of the palindrome
print('this is inverselist',inverselist)


if list == inverselist:
    print('Congratulations ', '"', possiblepalindrome, '"', ' is a palindrome!')
else:
    print('Unfortunately', '"', possiblepalindrome, '"', 'is not a palindrome.')

相关问题 更多 >