2013-03-22 116 views
0

我想從用戶那裏獲取輸入,並且想要將值存儲在矩陣中。我正在使用Python 2.7並獲取輸入的代碼。但我正在逐漸其中指出的錯誤:在Python 2.7中獲取矩陣輸入

Traceback (most recent call last): 
    File "C:/.../sim.py", line 18, in <module> 
    print 'Please enter the equitities' 
    File "<string>", line 1, in <module> 
NameError: name 'AAPL' is not defined 

這裏是我的代碼:

ls_symbols_cnt = input('Enter the number of equities') 
i = 0 
n = 0 
print 'Please enter the equities' 
while n<=ls_symbols_cnt: 
    ls_symbols[i,n] = input('Enter equity %n') 
    n =+ 1 

難道我做錯了什麼?有沒有辦法使用numpy來獲取矩陣的值?感謝您的輸入。

改變它的raw_input來後,我得到這個錯誤:

Traceback (most recent call last): 
    File "C:/Users/Rohit/Downloads/Computational_Investing/Hw-1/sim.py", line 21, in <module> 
    ls_symbols[i,n]= raw_input('Enter equity') 
NameError: name 'ls_symbols' is not defined 

我不知道爲什麼它說沒有定義的名稱。感謝您的輸入!

+0

'n = + 1'應該是'n + = 1'。 – unutbu 2013-03-22 17:12:59

+0

謝謝。我改變了這一點。 – 2013-03-22 17:17:18

+0

您需要有一個數組來放入數據。嘗試添加'ls_symbols = np.empty((1,ls_symbols_cnt))',這可能會導致您遇到不同的類型轉換錯誤。你期待用戶輸入什麼?數字?字符串? – Jaime 2013-03-22 17:49:13

回答

1

input()將嘗試評估用戶輸入的內容。這是一個令人困惑的命名功能。你想raw_input()。

例如,如果用戶輸入「2 + 3」:

raw_input() 
    => 5 

input() 
    => "2+3" 

注意,如果你希望字符串以外的東西,你必須自己執行轉換。如果用戶輸入「42」並且你想要數字42而不是字符串「42」,你需要int(raw_input())。