2013-03-27 60 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