输入()

2024-10-02 14:18:41 发布

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

我发现了这段代码,并尝试在python3上使用它,但它在geeks IDE上不起作用

list=input('racecar:')

if (list==list[::-1]):
    print ("It is a palindrome")
else:
   print("it is not palindrome")

我得到了:

list=input('racecar:') EOFError: EOF when reading a line


Tags: 代码inputifisnotitideelse
3条回答

只有在input不可用或在调用期间突然关闭时,EOFError: EOF when reading line才可能在input调用期间发生

既然您提到您是从geeks IDE使用它,我假设它没有针对用户输入重定向{}

请尝试使用cmdpython <file>在终端上以交互方式运行代码

您希望使用input接受用户输入,但可能没有将其提供给脚本。在

相反,尝试在尝试接受用户输入之前直接设置值。在

test_values = [
    "non-palindrome",
    "123321",
]

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

for value in test_values:
    if palindrometest(value):
        print("{} is a palindrome".format(value))
    else:
        print("{} is not a palindrome".format(value))

一旦您完成了这项工作,请尝试在脚本中单独使用input来获得关于它如何工作的良好感觉。在

^{pr2}$

这是因为您可能正在使用python2.7。我刚刚在python3.6上测试了您的代码,它运行得很好(我确实将变量列表更改为lis)。在

相关问题 更多 >