简体   繁体   English

名称“USER_INPUT”未定义? (Python 3.9.2)

[英]Name 'USER_INPUT' is not defined? (Python 3.9.2)

I'm trying to make a Python program which takes in a string and evaluates whether it's a palindrome (reads the same backwards) or not.我正在尝试制作一个 Python 程序,该程序接受一个字符串并评估它是否是回文(向后读取相同)。 I've tried to extend it by not allowing numbers to come as an input, and that part works fine.我试图通过不允许数字作为输入来扩展它,并且该部分工作正常。

a = eval(input('Put a word here: '))

if type(a) == int or float:
    print('That\'s a number man.')
    exit()

b = a[::-1]
if a == b:
    print('The word is a palindrome!')
else:
    print('The word is not a palindrome!')

However, when I run the program with a random word, such as 'fries', as an input in cmd (using Windows, Python 3.9.2), I get this error:但是,当我使用随机词(例如“薯条”)作为 cmd 中的输入运行程序时(使用 Windows、Python 3.9.2),我收到此错误:

Traceback (most recent call last):
  File "C:\Users\Azelide\Desktop\folderr\hello.py", line 1, in <module>
    a = eval(input('Put a word here: '))
  File "<string>", line 1, in <module>
NameError: name 'fries' is not defined

I've seen people getting this error when running Python 2 and using input() instead of raw_input(), that should not be a problem in Python 3 though.我看到有人在运行 Python 2 并使用 input() 而不是 raw_input() 时遇到此错误,但这在 Python 3 中应该不是问题。 By the way, when I omit the part of the code which excludes numbers from the input, the palindrome checker works fine.顺便说一句,当我省略从输入中排除数字的代码部分时,回文检查器工作正常。 Any ideas?有任何想法吗?

As mentioned in the comments, your first condition always evaluates to true如评论中所述,您的第一个条件始终评估为 true

try this:尝试这个:

a = input('Put a word here: ')

for char in a:
    if char.isdigit():
        print('That\'s a number man.')
        exit()

b = a[::-1]
if a == b:
    print('The word is a palindrome!')
else:
    print('The word is not a palindrome!')

output: output:

Put a word here: fries
The word is not a palindrome!

I have managed to solve it now, while extending it to not allow number-letter combinations.我现在设法解决了它,同时将其扩展为不允许数字字母组合。

a = input('Put a word here: ')

try:
    float(a)
    print('That\'s a number man.')
    exit()
except ValueError:
    for char in a:
        if char.isdigit():
            print('That\'s a combination of letters and numbers.')
            exit()

b = a[::-1]
if a == b:
    print('The word is a palindrome!')
else:
    print('The word is not a palindrome!')

I will tell you what is wrong in your code the function eval takes a string so that if the string could be a function it will make the function else it will raise error and when you made the function input inside it, this returns a value if this value == a function then make the function else raise error like this I will tell you what is wrong in your code the function eval takes a string so that if the string could be a function it will make the function else it will raise error and when you made the function input inside it, this returns a value if此值 == 一个 function 然后使 function 否则引发这样的错误

a = eval(input('enter a name: '))

now if the user enters a value which can not be a function it will raise error like this现在如果用户输入一个不能是 function 的值,它将引发这样的错误

name'value that the user input' is not defined

now you can make as what the people said up现在你可以按照人们所说的那样做

a = input('Put a word here: ')

try:
    float(a)
    print('That\'s a number man.')
    exit()
except ValueError:
    for char in a:
        if char.isdigit():
            print('That\'s a combination of letters and numbers.')
            exit()

b = a[::-1]
if a == b:
    print('The word is a palindrome!')
else:
    print('The word is not a palindrome!')

    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM