要列出的字符串和循环检查

2024-04-26 11:24:20 发布

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


给定一个包含N个用户提供的字符串的列表,任务是打印每个字符串是否为回文((Python)

我已经有这个密码了。并不断告诉我错误“回溯(最近一次电话):

  File "C:\Users\jpsam\Desktop\fuckmylife.py", line 20, in <module>
    palindrome_checker(q)
  File "C:\Users\jpsam\Desktop\fuckmylife.py", line 4, in palindrome_checker
    while y <len(inputlist()):
TypeError: 'list' object is not callable"

def palindrome_checker(q):

   y = 0
    while y <len(inputlist()):
        if inputlist == inputlist[::-1]:
            print(q, " is a panlindrome")
            len(inputlist()-1)
        else:
            print (q, "not a palindrome")
            len(inputlist()-1)
        return (q)

x = 1
inputlist = []
while x == 1:
    q = input("Input string: ")
    inputlist.append(q)
    x = int(input("Do you want to add more? [1]YES [0]NO ====>"))

palindrome_checker(q)

Tags: 字符串inpylenislinecheckerusers
1条回答
网友
1楼 · 发布于 2024-04-26 11:24:20

首先,缩进不正确

其次,语句len(inputlist()-1)给出了异常,因为您在一个列表inputlist后面加了(),它要求解释器将inputlist作为函数调用。不幸的是,我不知道这条语句应该实现什么(不是if分支中的另一条——您的代码也可以重构)

更简单的回文函数可能如下所示:

def palindrome(s):
    return s == s[::-1]

您可以按如下方式使用它:

if palindrome("able I was ere I saw elba"):
    print("The phrase is palindromic")

相关问题 更多 >