9/13 Something of Value


#1

hello, the error says that it's not printing out the correct total...what did i not do properly?


#2

you have to keep adding to the existing value of total each time but you are changing the value of total each time
You ought yo do

total = total + (pices*stock) # total+=(pices*stock)

you could try something like this

Edit:

A correction made with regards to @ionatan excerpt below

def total():
    total = 0
    for i in stock:
        total += (prices[i] * stock[i])
    return total
print total()

or

def total(price, stocks):
    total = 0
    for i in stock:
        total += (prices[i] * stock[i])
    return total
print total(prices, stock)

or you can do it without encapsulating it in a function

total = 0
for i in stock:
    total  = total + (prices[i] * stock[i])
print total

#3

thank you very much. i also appreciate you giving me an explanation!


#4

I received the correct answer not sure why adding the total to the total variable was valid aspect. It was set to 0 so how would that change the value?

total=0
for key in prices:
total=prices[key]*stock[key]+total
print total


#5

I'm also don´t understand why my code is wrong?

prices = {
    "banana" : 4,
    "apple"  : 2,
    "orange" : 1.5,
    "pear"   : 3,
}
stock = {
    "banana" : 6,
    "apple"  : 0,
    "orange" : 32,
    "pear"   : 15,
}

for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]

total = 0
for total_stock in prices:
    total = prices[total_stock] * stock[total_stock]
    print total

Until my understand we calculated the price of every product... so, ....I don´t understand. :sweat_smile:


#6

Here's my code, it worked.

prices = {
"banana" : 4,
"apple"  : 2,
"orange" : 1.5,
"pear"   : 3,
}
stock = {
"banana" : 6,
"apple"  : 0,
"orange" : 32,
"pear"   : 15,
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
total = 0
for key in prices:
stock_cost = prices[key] * stock[key]
print stock_cost
total += stock_cost
print total

#7

thanks rydan..it's great to see people like you who wants to share and explain completely about the solutions...


#8

@mrugerio this is your code corrected

prices = {
    "banana" : 4,
    "apple"  : 2,
    "orange" : 1.5,
    "pear"   : 3,
}
stock = {
    "banana" : 6,
    "apple"  : 0,
    "orange" : 32,
    "pear"   : 15,
}

for key in prices:
    print (key)
    print ("price: %s" % prices[key])
    print ("stock: %s" % stock[key])

total = 0
for total_stock in prices:
    total += prices[total_stock] * stock[total_stock] #this line was the issue
print (total)

you were just changing the value of total to be prices[total_stock] * stock[total_stock] at each iteration instead of adding to it and changing its value in memory


#9

That doesn't do what you think it does!

Iterating through a dictionary means iterating through its keys.
The prices and stock dictionaries have the same set of keys, therefore it is sufficient to iterate through only one of them. If you in any way iterated through both, then you would be doing something different from what you want.

Luckily, you have a second mistake which cancels out your first.

This expression:

prices and stock

Evaluates to:

stock

You can try this yourself:

print 5 and 6

That evaluates to 6

Similarly, this evaluates to 5:

print 5 or 6

The and and or operators don't magically combine things, they are very simple. This is what they do:

and:
Return the left operand if it is falsy, otherwise return the right side.

or:
Return the left operand if it is truthy, otherwise return the right side.

A non-empty dictionary is truthy:

print bool({4: 4})

Whereas "empty" things tend to be falsy:

print bool({})

#10

This is also a bad suggestion. You should (almost) never use mutable values as default argument values, because there's only one instance of that default value, so if you modify the default value, that modification will be there next time you call the function as well.

If you really want to have default values that are mutable, then use None and if they are None at the beginning of the function then assign empty dictionaries or what have you to those names.

And again, the and-operator doesn't work that way.

You should also put spaces around +=, especially when suggesting how others should write code (PEP8 has a bunch of recommendations on how code should look like and most agree with that it's a good idea to follow them)

And what you have there are functions, methods are a lot like functions but they can be bound to objects (they are aware of some data) - whenever you are typing a name, and then a dot . operator and then a name followed by parentheses (), that's a method.

It's great that you are helping, just please please be careful with what you say!


#11

total = 0
for key in prices:
total += prices[key] * stock[key]
print total

worked for me, but as novice I do not know if this is the very best solution?


#12

What does the += operator mean

And what seems to be the issue with this code

prices = {
"banana" : 4,
"apple"  : 2,
"orange" : 1.5,
"pear"   : 3,

}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
total = 0
for stuff in prices:
stock_cost = prices[stuff] * stock[stuff]
print total + stock_cost


#14

You add the product of prices and stock to total as total is 0. By doing this, you update the "total" value to reflect the product of prices * stock.

prices * stock + total is the value for "total"

Make sense?


#15

Thanks it worked. I get how it worked. your a good help. :grinning:


#16

my code
prices = {“banana”: 4,“apple”: 2,“orange”: 1.5,“pear”: 3}

stock = {“banana”: 6, “apple”: 0, “orange”: 32, “pear”: 15}

total = 0
for food in prices:
print prices[food] * stock[food]
total = total + prices[food] * stock[food]
print total

it works:grinning:


#18