Something of Value (math?) issues


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/1/4?curriculum_id=4f89dab3d788890003000096#

Hey guys!
I am unsure of what this code actually wants out of me at this point, it is printing the proper total and following all the instructions (that I can tell), yet am greeted with the area: Oops, try again. It looks like your code did not print the correct total.


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:
    value = prices[key] * stock[key]
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]
    print value
    total += value

print "total: %s" % total


#2

I solved the issue. It was throwing an error due to the fact that I had:
print "total: %s" % total
Instead of:
print total

I am unsure of why it told me "total" was wrong, even though the values stored were the same... Oh well, onto the next!


#3

Don't print interim values. Just accumulate a total and print that. If the SCT still rejects your response, then print just the total, wtih no formatting or additional text.

Aside

Variables use memory and never give it back if they are not inside a function (which does garbage collection on exit). Unnecessary variables add code bloat and nothing much else, something to consider going forward.

total += prices[iey] * stock[key]

#4

The only reason I printed the value was because I wasn't sure why my program wasn't 'correct,' and I thought it might be because I wasn't printing the 'value.' For example: I wrote 'cost' instead of 'price' in one of my programs and it gave me a fit. Code Academy is phenomenal, but it has it's little flaws like that.

As for my variable use, I only had it in there to make sure it was doing math correctly all the way through just so that I could monitor if the code was breaking, and if so where.

Like I said, code academy can be finicky, and the "issue" with my code was I coded print "total: %s" % total," instead of just "print total"


#5

Most of the courses in the old tracks are written by volunteers (members) and not by Codecademy. We are free to use the site but should lower our expectations given that this is not a formal learning environment, only a scratch on the surface of what is really out there to learn.

When posting your code in a question, be sure to remove the debugging code you added so we can examine the actual code. And, if you want people to help you, be sure to credit them when they give you correct advice.


#6

I am aware, and as I said, the site is phenomenal. But it does have small quirks and flaws, that is nothing against the site.

Moving forwards I will consider this