2013-03-27 41 views
0

因此,我正在学习python,从Java背景,为我的新工作,我遇到了以下错误。基本的Python模块错误

我试着在终端上(使用mac)运行这个模块,然后通过空闲。都是徒劳的。

userName = input("What is your name?: ") 
    lengthName = str(len(usernName)) 

    yourName = "\nYour name is " + userName + " and is " + legnthName + " letters long." 

    print(yourName) 
    input("\nPress enter to exit") 

当此在终端被执行,I型为第一输入: “约翰” 我得到这个结果:

File "[...]", line 1, in <module> 
    userName = input("What is your name?: ") 
    File "<string>", line 1, in <module> 
    NameError: name 'John' is not defined 

类似地,当经由IDLE执行:

Traceback (most recent call last): 
    File "/Users/zachmartin/Desktop/python fails/lol.py", line 2, in <module> 
     lengthName = str(len(usernName)) 
    NameError: name 'usernName' is not defined 

这是怎么回事?

回答

4

两个问题:

  • 您应该使用raw_input()而不是input()(因为input()会将您键入的内容为Python代码,并试图对其进行评估)。

  • 你有一个错字,userNameusernName(注意额外的n)。 (后面还有legnthName。)

+0

果然,谢谢! – MITjanitor 2013-03-27 19:02:07