在Python中运行简单输入任务时出错

在Python中运行简单输入任务时出错,python,Python,我正在尝试运行以下几行简单的Python代码: myName = input("What is your name?") print (myName) if (myName == 'Omar'): print ("Omar is great!") else: print ("You are ok") 如果我使用IDLE运行它,它可以正常运行,但一旦我将它放入扩展名为“.py”的文本文件中,我会得到以下输出和错误: What is your name?omar Tracebac

我正在尝试运行以下几行简单的Python代码:

myName = input("What is your name?")
print (myName)

if (myName == 'Omar'):
    print ("Omar is great!")
else:
    print ("You are ok")
如果我使用IDLE运行它,它可以正常运行,但一旦我将它放入扩展名为“.py”的文本文件中,我会得到以下输出和错误:

What is your name?omar
Traceback (most recent call last):
  File "hello.py", line 2, in <module>
    myName = input("What is your name?")
  File "<string>", line 1, in <module>
NameError: name 'omar' is not defined
你叫什么名字?奥马尔 回溯(最近一次呼叫最后一次): 文件“hello.py”,第2行,在 myName=输入(“你叫什么名字?”) 文件“”,第1行,在 名称错误:未定义名称“omar” 我不知道需要定义“omar”的错误状态,即使“omar”只是一个字符串输入


非常感谢您的帮助。

使用
原始输入

myName = raw_input("What is your name?")
Python3中使用了
input
,其Python2等价物是
raw\u input
input通过调用
eval
来包装
raw\u input
,这是错误的根源。您可能正在使用IDLE和命令提示符的不同版本,但没有意识到这一点


如果您需要一个跨python的解决方案,您可能想看看这个问题。

看起来您的IDLE配置为使用python 3,它对input()函数有很好的性能,但是您正在从命令行运行python 2。在Python2中,
input
并不能实现您期望的功能,您应该使用
raw\u input
。(
raw\u input
以字符串形式返回输入,这是您想要的)

请添加几句话来解释您的代码在做什么,这样您就可以为您的答案获得更多的投票。欢迎使用堆栈溢出!虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
#this is a way of using raw_input
pswrd = raw_input("enter the password ")
if pswrd == "im cool": print "you may pass"