2013-11-15 36 views
3
PizzaChange=float(input("What would you like the new price for all standard pizzas to be? "))  
PriceList[0][1][2][3][4][5][6]=[PizzaChange] 
PriceList[7][8][9][10][11]=[PizzaChange+3] 

基本上我有一個輸入,一個用戶將放置多個值(浮動輸入)進,那麼將所有這些前述的列表索引的設置爲該值。出於某種原因,我不能得到它設置他們沒有拿出一個:類型錯誤:「浮動」對象未標化的

TypeError: 'float' object is not subscriptable 

錯誤。我做錯了什麼,或者我只是看錯了方向?

+1

什麼行會產生錯誤? – ASGM

回答

3

PriceList[0]是一個浮動。 PriceList[0][1]正試圖訪問浮動的第一個元素。相反,做

PriceList[0] = PriceList[1] = ...code omitted... = PriceList[6] = PizzaChange 

PriceList[0:7] = [PizzaChange]*7 
+0

從0到7的一個切片有7個元素,而不是6. – roippi

+0

非常感謝紳士們。 – Beardo

0

您沒有使用PriceList [0] [1] [2] [3] [4] [5] [6]選擇多個索引,而是每個[]都進入子索引。

試試這個

PizzaChange=float(input("What would you like the new price for all standard pizzas to be? "))  
PriceList[0:7]=[PizzaChange]*7 
PriceList[7:11]=[PizzaChange+3]*4 
+0

問題在於它只用一個va來代替那些索引略。例如我希望列表= [1,2,3,4,5,6,7,8,9,10,11] eqaul當我在我的輸入中說3時List = [3,3,3,3, 3,3,3,7,8,9,10,11。 – Beardo

+0

看到我上面的編輯,只是乘以你正在替換的物品數量 – Pruthvikar

2
PriceList[0][1][2][3][4][5][6] 

這是說:去我收集PriceList的第一個項目。那件事是一個集合;得到它的第二個項目。那件事是一個集合;獲得第3 ...

相反,你要切片

PriceList[:7] = [PizzaChange]*7 
0
PizzaChange=float(input("What would you like the new price for all standard pizzas to be? "))  
for i,price in enumerate(PriceList): 
    PriceList[i] = PizzaChange + 3*int(i>=7) 
0

它看起來像你想設置元素0至價目表的11爲新值。語法通常是這樣的:

prompt = "What would you like the new price for all standard pizzas to be? " 
PizzaChange = float(input(prompt)) 
for i in [0, 1, 2, 3, 4, 5, 6]: PriceList[i] = PizzaChange 
for i in [7, 8, 9, 10, 11]: PriceList[i] = PizzaChange + 3 

如果他們總是連續的範圍內,那麼它寫的更簡單:

prompt = "What would you like the new price for all standard pizzas to be? " 
PizzaChange = float(input(prompt)) 
for i in range(0, 7): PriceList[i] = PizzaChange 
for i in range(7, 12): PriceList[i] = PizzaChange + 3 

僅供參考,PriceList[0][1][2][3][4][5][6]指的是「元6元的元件5的4,元素1中元素3的元素3的PriceList。換言之,它與((((((PriceList[0])[1])[2])[3])[4])[5])[6]相同

相關問題