python TypeError:“float”对象不可订阅

yeotifhr  于 2023-02-07  发布在  Python
关注(0)|答案(6)|浏览(141)
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

错误。我是做错了什么还是我看问题的方式错了?

pxq42qpu

pxq42qpu1#

PriceList[0]是浮点型。PriceList[0][1]试图访问浮点型的第一个元素。请改为

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

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

i1icjdpr2#

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

这是说:转到我的集合PriceList的第一个项目。那是一个集合;得到它的第二项。那是一个集合;得到第三个...
相反,您需要 * 切片 *:

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

nx7onnlm3#

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)
c86crjj0

c86crjj04#

您似乎正在尝试将PriceList的元素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]表示“PriceList的元素5的元素6的元素4的元素3的元素2的元素1的元素0的元素。换句话说,它与((((((PriceList[0])[1])[2])[3])[4])[5])[6]相同。

v7pvogib

v7pvogib5#

您没有选择价格表为[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
t0ybt7op

t0ybt7op6#

我得到了下面相同的错误:
TypeError:"float"对象不可订阅
因为我在MoneyField()Django-money中分别将带小数部分的数字设置为MinMoneyValidator()MaxMoneyValidator(),如下所示:

# "models.py"

from django.db import models
from djmoney.models.fields import MoneyField
from djmoney.models.validators import MinMoneyValidator, MaxMoneyValidator 

class Product(models.Model):
    name = models.CharField(max_length=50)
    price = MoneyField( # Here
        max_digits=5, decimal_places=2, default=0, default_currency='USD',
        validators=[        # Here                    # Here
            MinMoneyValidator(0.00), MaxMoneyValidator(999.99),
        ]
    )

因此,我在MoneyField()中将不带小数部分的数字分别设置为MinMoneyValidator()MaxMoneyValidator(),如下所示,然后解决了上述错误:

# "models.py"

from django.db import models
from djmoney.models.fields import MoneyField
from djmoney.models.validators import MinMoneyValidator, MaxMoneyValidator

class Product(models.Model):
    name = models.CharField(max_length=50)
    price = MoneyField(
        max_digits=5, decimal_places=2, default=0, default_currency='USD',
        validators=[       # Here                 # Here
            MinMoneyValidator(0), MaxMoneyValidator(999),
        ]
    )

相关问题