An iteration with for
in a dictionary always iterates only on the keys - so you didn’t see the values.
Dictionaries, however, in addition to being directly iterable have three methods that return specialized iterators: over the keys (.keys()
), on values (.values()
) or about both (.items()
) - this last method returns the keys and sequence values of two items (tuples) - and can be used with the Augmented Python assignemnt that allows multiple variables to receive the item values of a sequence.
So you can do it like this:
lanchonete = {"Salgado":4.50, "Lanche":6.50,"Suco":3.00,"Refrigerante":3.50,"Doce":1.00}
for produto, preco in lanchonete.items():
print(produto, preco)
If you want to add more symformations to what you are printing, a good request are the f-strings, which exist from Python 3.6:
for produto, preco in lanchonete.items():
print(f"Produto {produto}: R${preco:0.02f}")
Poxa. I appreciate the help. Thank you very much.
– Rodrigo Ferraz