Algo de valor.Un día en el super


#1

<Debajo de esta línea, agrega un hipervínculo / link al ejercicio EXACTO del cual tengas problemas.>

<Dinos de qué modo no está funcionando tu código. Incluye TODOS los mensajes de errores aquí abajo.>
Hola a tod@s.
Tengo un problema con mi código que me devuelve:
Traceback (most recent call last):
File “python”, line 16, in
TypeError: ‘int’ object is not iterable

El cuál surge en la línea donde defino una variable como la suma de la multiplicación obtenida en ambos diccionarios. Si pueden darme una pista de como resolver esto, se lo agradecería.

Saludos.

```

precios = {
“banano”: 4,
“manzana”: 2,
“naranja”: 1.5,
“pera”: 3
}

inventario = {
“banano”: 6,
“manzana”: 0,
“naranja”: 32,
“pera”: 15
}

for p in precios:
precio= sum(precios[p] * inventario[p])
print precio

<No elimines los tres acentos abiertos de arriba.>

#2

With a google search
sum() ‘int’ object is not iterable site:stackoverflow.com
you would learn that sum() can only be used
like

print sum([1,2,3,4])
#or
print sum((1,2,3,4))

So change your code into

precio= precios[p] * inventario[p]

#3

Thank you leonhard.
But what can I do to get the total sum from both dictionaries using a bucle for?
precios = {
“banano”: 4,
“manzana”: 2,
“naranja”: 1.5,
“pera”: 3
}

inventario = {
“banano”: 6,
“manzana”: 0,
“naranja”: 32,
“pera”: 15
}
PRECIOS * INVENTARIO
“banano”: 4 * 6 = 24
“manzana”: 2 * 0 = 0
“naranja”: 1.5 * 32 = 48
“pera”: 3 * 15 = 45
TOTAL = 117


#4

Something like this ??

def busca_precios():
    #create EMPTY list/array
    # mi_lista = [] or you use
    mi_lista = list()
    
    for p in precios:
        mi_lista.append(precios[p] * inventario[p])
    print "mi_lista ",mi_lista
    print "sum      ",sum(mi_lista)

busca_precios()

Something to read
https://stackoverflow.com/questions/10487278/how-to-declare-and-add-items-to-an-array-in-python


#5

Or in a nicer form

def busca_precios():
    #create EMPTY list/array
    # mi_lista = [] or you use
    mi_lista = list()
    
    for p in precios:
        #if len(p) < 7:
        #    t1 = "\t\t"
        #else:
        #    t1 = "\t"
        #print p,t1,precios[p]," * ",inventario[p],"\t= ",precios[p] * inventario[p]
        print "{:<10}".format(p),"{:<4}".format(precios[p])," * ","{:<4}".format(inventario[p])," = ","{:5.2f}".format(precios[p] * inventario[p])
        mi_lista.append(precios[p] * inventario[p])
    #print "\t\t\t\t+ ====="
    print " "*25,"+ ======"
    #print "\t\t\t     sum:",sum(mi_lista)
    print " "*21,"sum =","{:5.2f}".format(sum(mi_lista))
    print "mi_lista ",mi_lista

busca_precios()

Something to read
https://stackoverflow.com/questions/20309255/how-to-pad-a-string-to-a-fixed-length-with-spaces-in-python

https://stackoverflow.com/questions/10837017/how-do-i-make-a-fixed-size-formatted-string-in-python


#6

Thank you leonhard.
This form works good for me and the other one looks really good but I’m still being a beginer in python so I chose this first option.