' float'对象不可订阅 - 列表编码

发布于 2025-02-07 02:09:54 字数 1271 浏览 1 评论 0原文

acumSubTotal = 0
acumIva = 0
acumTotalProd = 0

listatotalProd=[]
listasubTotal = []
listacodProducto= []
listanomProducto=[]
listacanProducto=[]
listavalUnitario=[]
listatipoIva=[]
N = int(input("Digite la cantidad de Artículos a facturar: "))

for item in range(N):
    i = item + 1
    listacodProducto.append(int(input(f"Digite el código del producto {item + 1}: ")))
    listanomProducto.append((input(f"Digite el nombre del producto {item + 1}: ")))
    listacanProducto.append(float(input(f"Digite la cantidad del producto {item + 1}: ")))
    listavalUnitario.append(float(input(f"Digite el valor unitario del producto {item + 1}: ")))
    listatipoIva.append(float(input(f"Tipo de IVA (que puede ser: 1: Exento de IVA, 2: Bienes, IVA 5%, 3: General, IVA 19%) del producto {item + 1}: ")))

    listasubTotal.append(listavalUnitario[item] * listacanProducto[item])

    if listatipoIva[item] == 1:
        listatipoIva = 0
    elif listatipoIva[item] == 2:
        listatipoIva = listasubTotal[item] * 0.05   
    else :
        listatipoIva = listasubTotal[item] * 0.19
   

    listatotalProd.append(listatipoIva[item] + listasubTotal[item])

我找不到这个问题的原因。我刚开始,我无法弄清楚什么是错误。西班牙语中的代码希望您不介意。帮助!!

该错误是在“ float”对象下的最后一行上特别显示的错误。

acumSubTotal = 0
acumIva = 0
acumTotalProd = 0

listatotalProd=[]
listasubTotal = []
listacodProducto= []
listanomProducto=[]
listacanProducto=[]
listavalUnitario=[]
listatipoIva=[]
N = int(input("Digite la cantidad de Artículos a facturar: "))

for item in range(N):
    i = item + 1
    listacodProducto.append(int(input(f"Digite el código del producto {item + 1}: ")))
    listanomProducto.append((input(f"Digite el nombre del producto {item + 1}: ")))
    listacanProducto.append(float(input(f"Digite la cantidad del producto {item + 1}: ")))
    listavalUnitario.append(float(input(f"Digite el valor unitario del producto {item + 1}: ")))
    listatipoIva.append(float(input(f"Tipo de IVA (que puede ser: 1: Exento de IVA, 2: Bienes, IVA 5%, 3: General, IVA 19%) del producto {item + 1}: ")))

    listasubTotal.append(listavalUnitario[item] * listacanProducto[item])

    if listatipoIva[item] == 1:
        listatipoIva = 0
    elif listatipoIva[item] == 2:
        listatipoIva = listasubTotal[item] * 0.05   
    else :
        listatipoIva = listasubTotal[item] * 0.19
   

    listatotalProd.append(listatipoIva[item] + listasubTotal[item])

I cannot find a reason for this problem. I'm just getting started and I can't figure out what is the error. Code's in spanish hope you don't mind. Help!!

The error is showing specifically on the last line under 'float' object is not subscriptable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉薄对峙 2025-02-14 02:09:54

正在执行的列表中

listatipoIva = some_number

问题在于,您将listatipoiva定义为列表,然后在您尝试访问listatipoiva [item]的结尾时 循环您看到的错误是因为ListAtipoiva不再是可订阅的对象。

可订阅的对象是实现__ getItem __()方法的对象,例如:字符串,列表,元组和词典。不是漂浮或ints。

您需要研究这种结构:


    if listatipoIva[item] == 1:
        listatipoIva = 0
    elif listatipoIva[item] == 2:
        listatipoIva = listasubTotal[item] * 0.05   
    else :
        listatipoIva = listasubTotal[item] * 0.19

并弄清楚您想做什么。如果我正确理解,则根据所选用户输入(1、2或3),您要在listAsubtotal值上操作。也许您正在寻找类似的东西:

     if listatipoIva[item] == 1:
        listasubTotal[item] = 0
    elif listatipoIva[item] == 2:
        listasubTotal[item] =* 0.05   
    else :
        listasubTotal[item] =* 0.19

它将根据所选的IVA选项更改ListAsubtotal [item]的值。

作为建议,要改善代码可读性,请使用snake_case或如果您真的想使用骆驼箱一致使用(listAcanAcanProducto而不是listAcanProducto

The problem is that you are defining listatipoIva as a list but then in you if-statement you are doing

listatipoIva = some_number

When you try to access listatipoIva[item] at the end of you for loop the error you are seeing is because listatipoIva is no longer a subscriptable object.

Subscriptable objects are those that implement the __getitem__() method like: strings, lists, tuples, and dictionaries. NOT floats or ints.

You need to look into this structure:


    if listatipoIva[item] == 1:
        listatipoIva = 0
    elif listatipoIva[item] == 2:
        listatipoIva = listasubTotal[item] * 0.05   
    else :
        listatipoIva = listasubTotal[item] * 0.19

And figure out what you want to do. If I understand right, depending on the selected user input (1, 2 or 3) you want to operate on the listasubTotal value. Maybe you are looking for something like this:

     if listatipoIva[item] == 1:
        listasubTotal[item] = 0
    elif listatipoIva[item] == 2:
        listasubTotal[item] =* 0.05   
    else :
        listasubTotal[item] =* 0.19

That will change the value of listasubTotal[item] depending on the selected IVA option.

As a recommendation, to improve code readability use snake_case or if you really want to use camelCase use it consistently (listaCanProducto instead of listacanProducto)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文