Part 11 | Python Programming | Creating and working with Dictionaries

Опубликовано: 11 Февраль 2025
на канале: oOSylarTechOo
153
7

This is part 11 of a series of python coding I am having fun with and want to show others interested or stuck.

Code Example:

#Create your dictionary which is separated by a ":" called a key_value pair
prices = {
"box_of_spaghetti" : 4,
"lasagna" : 5,
"hamburger" : 2
}

quantity = {
"box_of_spaghetti" : 6,
"lasagna" : 10,
"hamburger" : 0
}

#Create a rolling sum variable with the value of 0
money_spent = 0

#Then create a for loop that will add up each price * quantity
#that will provide a total spent
for i in quantity:
money_spent = money_spent + (prices[i] * quantity[i])

#print the total spent
print(money_spent)

#Add a new item to the dictionary
prices['hot dogs'] = 2
print(prices)

#Do the same for quantity, otherwise the loop will not work
quantity['hot dogs'] = 10
print(quantity)

#Run the loop again
money_spent = 0

for i in quantity:
money_spent = money_spent + (prices[i] * quantity[i])

print(money_spent)

#As you can see the new items have been added and changed
#the total spent on the items
#Good Luck and have fun!

#python #pythonprogramming #pythontutorial #pythonforbeginners #shorts

Music: Paid Subscription with Wondershare Filmora