2010-08-16 58 views
2
#!usr/bin/python 

listofnames = [] 

names = input("Pls enter how many of names:") 
x = 1 

for x in range(0, names): 
    inname = input("Enter the name " + str(x)) 
    listofnames.append(inname) 


print listofnames 

錯誤我的python代碼有什麼問題?

inname = input("Enter the name " + str(x)) 

文件 「」,第1行,在 NameError:名稱 '約翰' 沒有定義

+0

請注意,您可以使用列表理解來做到這一點;代碼模式'var = []; for ...:var.append(...)'是線索。 'listofnames = [raw_input(「Name%d?」%x)for x in range(int(raw_input(「How many names?」)))]'。 – katrielalex 2010-08-16 09:10:57

回答

1

input從其隨後解釋爲Python的用戶得到的文本代碼(因此它試圖評估你輸入的東西,Jhon)。你需要raw_input他們兩個,你需要將輸入的數字(因爲它是一個字符串)轉換爲你的range的整數。

#!usr/bin/python 

listofnames = [] 
names = 0 
try: 
    names = int(raw_input("Pls enter how many of names:")) 
except: 
    print "Problem with input" 

for x in range(0, names): 
    inname = raw_input("Enter the name %d: "%(x)) 
    listofnames.append(inname) 

print listofnames 
+0

+1:我們在同一頁:)。在你的腳本中,你可能想要在int轉換中拋出異常,並檢查「ValueError」而不是捕獲所有異常。 – sdolan 2010-08-16 07:54:15

+0

好主意,我會打印一條錯誤消息。但我通常只是在quick'n'dirty腳本中捕獲所有異常,因爲它們可能會輸入CTRL-D並給我一個EOFError。 – paxdiablo 2010-08-16 08:03:32

+0

謝謝你「Paxdiablo」:)謝謝你們倆。 – Freeman 2010-08-16 08:17:28

4

改爲使用raw_input。見http://docs.python.org/library/functions.html#raw_inputinput將做與eval(raw_input(prompt))相同的事情,因此輸入Jhon將嘗試在文件內找到符號Jhon(不存在)。因此,對於您現有的腳本,您必須在提示中輸入'Jhon'(注意引號集),以便eval將該值轉換爲字符串。

以下是input文檔中的摘錄警告。

Warning

This function is not safe from user errors! It expects a valid Python expression as input; if the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation. (On the other hand, sometimes this is exactly what you need when writing a quick script for expert use.)

下面是修正版本:

#!usr/bin/python 

# The list is implied with the variable name, see my comment below. 
names = [] 

try: 
    # We need to convert the names input to an int using raw input. 
    # If a valid number is not entered a `ValueError` is raised, and 
    # we throw an exception. You may also want to consider renaming 
    # names to num_names. To be "names" sounds implies a list of 
    # names, not a number of names. 
    num_names = int(raw_input("Pls enter how many of names:")) 
except ValueError: 
    raise Exception('Please enter a valid number.') 

# You don't need x=1. If you want to start your name at 1 
# change the range to start at 1, and add 1 to the number of names. 
for x in range(1, num_names+1)): 
    inname = raw_input("Enter the name " + str(x)) 
    names.append(inname) 

print names 

注:這是Python2.x。正如其他答案中所解釋的,Python3.x修復了輸入與raw_input混淆的問題。

+0

謝謝你「Sdolan」:) – Freeman 2010-08-16 08:16:32

+0

@Freeman:不客氣。我在代碼中添加了一些關於變量命名和處理異常的註釋。 – sdolan 2010-08-16 08:26:44

0

在python3中,input()現在可以像raw_input()一樣工作。然而,要讓你的代碼與Python3一起工作,仍然需要進行一些更改

#!usr/bin/python3 

listofnames = [] 

names = int(input("Pls enter how many of names:")) 
x = 1 

for x in range(0, names): 
    inname = input("Enter the name " + str(x)) 
    listofnames.append(inname) 


print(listofnames) 
+0

嘿,謝謝你的解釋!我正在使用2.6.5 – Freeman 2010-08-16 08:36:44