python input() 没有按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21250351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:23:53  来源:igfitidea点击:

python input() not working as expected

python

提问by liv2hak

I am a python newbie.I am getting familiar with loops and tried this example from a book

我是 python 新手。我熟悉循环并从书中尝试过这个例子

while True:
        s = input('Enter something : ')
        if s == 'quit':
                break
        print('Length of the string is', len(s))
print('Done')

However the output is as follows

但是输出如下

Enter something : ljsdf
Traceback (most recent call last):
  File "trial_2.py", line 2, in <module>
    s = input('Enter something : ')
  File "<string>", line 1, in <module>
NameError: name 'ljsdf' is not defined

采纳答案by Christian

You have to use raw_input()instead (Python 2.x), because input()is equivalent to eval(raw_input()), so it parses and evaluates your input as a valid Python expression.

您必须raw_input()改用 (Python 2.x),因为input()等效于eval(raw_input()),因此它会将您的输入解析并评估为有效的 Python 表达式。

while True:
        s = raw_input('Enter something : ')
        if s == 'quit':
                break
        print('Length of the string is', len(s))
print('Done')

Note:

笔记:

input()doesn't catch user errors (e.g. if user inputs some invalid Python expression). raw_input()can do this, because it converts the input to a string. For futher information, read Python docs.

input()不捕捉用户错误(例如,如果用户输入了一些无效的 Python 表达式)。raw_input()可以这样做,因为它将输入转换为string. 如需更多信息,请阅读 Python 文档

回答by mhlester

you want raw_input()in python2

你想raw_input()在python2中

while True:
    s = raw_input('Enter something : ')
    if s == 'quit':
            break
    print 'Length of the string is', len(s)
print 'Done'

input()tries to evaluate (dangerously!) what you give it

input()试图评估(危险的!)你给它的东西

回答by falsetru

It seems like you're using Python 2.x, while the code is expected to be run in Python 3.x.

看起来您使用的是 Python 2.x,而代码预计将在 Python 3.x 中运行。

inputin Python 2.xevaluates the input string unlike inputin Python 3.x.

input在 Python 2.x 中评估输入字符串与inputPython 3.x不同。

回答by Vipul

Your code will work fine in python 3.x

您的代码将在 python 3.x 中正常工作

But if you are using python 2 you will have to input string using raw_input()

但是,如果您使用的是 python 2,则必须使用 raw_input() 输入字符串

while True:
    s = raw_input('Enter something : ')
    if s == 'quit':
        break
    print('Length of the string is', len(s))
print('Done')
while True:
    s = raw_input('Enter something : ')
    if s == 'quit':
        break
    print('Length of the string is', len(s))
print('Done')

回答by linzwatt

In Python 2.x input()is designed to return numbers, int or float depending on the input from the user, you can also enter variable names.

在 Python 2.xinput()中,根据用户的输入返回数字、整数或浮点数,您还可以输入变量名。

you need to use:

你需要使用:

raw_input('Enter something: ')

The error is caused because Python thinks that "ljsdf" is the name of a variable, and that's why it raises this exception:

错误是因为 Python 认为 "ljsdf" 是变量的名称,这就是它引发此异常的原因:

NameError: name 'ljsdf' is not defined

NameError: name 'ljsdf' is not defined

becuase "ljsdf" is not defined as a variable. :D

因为“ljsdf”未定义为变量。:D

raw_input()is safer to use, and then convert the input to whatever other type after :D

raw_input()使用更安全,然后在 :D 之后将输入转换为任何其他类型