如何解决回文,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','is not a palindrome.')
解决方法
list
和 inverselist
具有对同一个列表的引用。您需要克隆 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,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','is not a palindrome.')
,
您发布了太多代码,却懒得去测试。 =
不符合您的预期。它反转输入列表,这是一个可变对象。
reverselist
输出:
def reverselist(argument): #This is a function. We put in some list,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','o',',' ','w','r','d'] ['d','H'] ['d','H']
更改为list
。 - 查找如何反转序列。我们希望您在此处发帖之前进行适当的研究。
- 学习测试代码块。请参阅 debugging help 的这个可爱参考。
您的功能可以很简单:
word
,
首先,将列表命名为 list
很诱人,但在 Python 中很危险,因为您将无法调用 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 ','is not a palindrome.')
,
要检查一个字符串是否是回文:
v = str(input("Put in the possible palindromic statement here: "))
print (v == (v[::-1]))
只需输入,与相反的比较。如果它们相等,你会得到一个 True
(所以它是一个回文),否则得到一个 False
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。