Something of value, Help~!


#1

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}
total = 0
for key in prices:
total = prices[key]*stock[key]
print "total: %s" % total

The result is
total 48
total 45
total 24
total 0

Question : How can I get the prices[key]*stock[key] real total!!!?????


#2

@hungclo
In Python, we must pay attention to the identation.
It's not the same writing this:

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

Than this:

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

In the first case "print" is inside the loop, and it will print "total" in every iteration. In the second case, "print" is out of the loop, so it will only print the last value asigned to "total"


#3

I finished it. Thank you @pabloqug so much!!!!


#4

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

print "Total: %s" % total

I don't know what is the problem.. it is throwing an error saying " It looks like your code did not print the correct total".


#5

the total which I am getting is 117.0.. used calc too, that the answer.


#6

removed print "total: %s" % total.. and used print total & its working fine


#7

I have the same problem as quaker777.

While

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

# look below
print total

works flawlessly. However, when I try to do this:

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

# look below
print 'total: %s' % total

I get an error "It looks like your code did not print the correct total."
Why is that so?