TypeError:“浮动"对象不可下标

2022-01-09 00:00:00 python indexing list replace insert

问题描述

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]

基本上,我有一个输入,用户将输入一个数字值(浮点输入),然后它将所有上述列表索引设置为该值.出于某种原因,我无法在没有提出以下建议的情况下设置它们:

Basically I have an input that a user will put a number values (float input) into, then it will set all of these aforementioned list indexes to that value. For some reason I can't get it to set them without coming up with a:

TypeError: 'float' object is not subscriptable

错误.我做错了什么还是我只是看错了?

error. Am I doing something wrong or am I just looking at it the wrong way?


解决方案

PriceList[0] 是一个浮点数.PriceList[0][1] 正在尝试访问浮点数的第一个元素.相反,做

PriceList[0] is a float. PriceList[0][1] is trying to access the first element of a float. Instead, do

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

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

相关文章