Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45490 > unrolled thread
| Started by | Bradley Wright <bradley.wright.biz@gmail.com> |
|---|---|
| First post | 2013-05-17 21:19 -0700 |
| Last post | 2013-05-18 09:22 -0400 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
Two Dictionaries and a Sum! Bradley Wright <bradley.wright.biz@gmail.com> - 2013-05-17 21:19 -0700
Re: Two Dictionaries and a Sum! Spacelee <fjctlzy@gmail.com> - 2013-05-18 12:30 +0800
Re: Two Dictionaries and a Sum! nanobio <hektorlg@gmail.com> - 2013-05-17 22:20 -0700
Re: Two Dictionaries and a Sum! nanobio <hektorlg@gmail.com> - 2013-05-17 22:22 -0700
Re: Two Dictionaries and a Sum! Chris Angelico <rosuav@gmail.com> - 2013-05-18 15:10 +1000
Re: Two Dictionaries and a Sum! Roy Smith <roy@panix.com> - 2013-05-18 09:22 -0400
| From | Bradley Wright <bradley.wright.biz@gmail.com> |
|---|---|
| Date | 2013-05-17 21:19 -0700 |
| Subject | Two Dictionaries and a Sum! |
| Message-ID | <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> |
Confusing subject for a confusing problem (to a novice like me of course!)
Thx for the help in advance folks
I have (2) dictionaries:
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
Here's my instructions:
consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!)
HERES MY CODE:
for key in prices:
print prices[key]*stock[key]
HERES THE OUTPUT:
48.0
45
24
0
ISSUE:
I need to find a way to add all of those together...any pointers?
[toc] | [next] | [standalone]
| From | Spacelee <fjctlzy@gmail.com> |
|---|---|
| Date | 2013-05-18 12:30 +0800 |
| Message-ID | <mailman.1792.1368851449.3114.python-list@python.org> |
| In reply to | #45490 |
[Multipart message — attachments visible in raw view] — view raw
for key in prices.keys():
print prices[key]*stock[key]
On Sat, May 18, 2013 at 12:19 PM, Bradley Wright <
bradley.wright.biz@gmail.com> wrote:
> Confusing subject for a confusing problem (to a novice like me of course!)
> Thx for the help in advance folks
>
> I have (2) dictionaries:
>
> prices = {
> "banana": 4,
> "apple": 2,
> "orange": 1.5,
> "pear": 3
> }
>
> stock = {
> "banana": 6,
> "apple": 0,
> "orange": 32,
> "pear": 15
> }
>
> Here's my instructions:
>
> consider this as an inventory and calculate the sum (thats 4*6 = 24
> bananas!)
>
> HERES MY CODE:
>
> for key in prices:
> print prices[key]*stock[key]
>
> HERES THE OUTPUT:
>
> 48.0
> 45
> 24
> 0
>
> ISSUE:
> I need to find a way to add all of those together...any pointers?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
*Space Lee*
[toc] | [prev] | [next] | [standalone]
| From | nanobio <hektorlg@gmail.com> |
|---|---|
| Date | 2013-05-17 22:20 -0700 |
| Message-ID | <38fdb42f-b233-4da5-8936-7439072edfbd@googlegroups.com> |
| In reply to | #45490 |
total,amount=0,0 for key in prices.keys(): price=prices[key]*stock[key] total+=price print "%s %s" % (price,total)
[toc] | [prev] | [next] | [standalone]
| From | nanobio <hektorlg@gmail.com> |
|---|---|
| Date | 2013-05-17 22:22 -0700 |
| Message-ID | <c954581b-fa06-41ab-8d02-833269369cc5@googlegroups.com> |
| In reply to | #45490 |
total,amount=0,0
for key in prices.keys():
amount=prices[key]*stock[key]
total+=amount
print "%s %s" % (amount,total)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-05-18 15:10 +1000 |
| Message-ID | <mailman.1793.1368853832.3114.python-list@python.org> |
| In reply to | #45490 |
On Sat, May 18, 2013 at 2:19 PM, Bradley Wright
<bradley.wright.biz@gmail.com> wrote:
> Confusing subject for a confusing problem (to a novice like me of course!)
> Thx for the help in advance folks
>
> I have (2) dictionaries:
>
> prices = {
> "banana": 4,
> "apple": 2,
> "orange": 1.5,
> "pear": 3
> }
>
> stock = {
> "banana": 6,
> "apple": 0,
> "orange": 32,
> "pear": 15
> }
>
> Here's my instructions:
>
> consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!)
Let me reword your problem a little, maybe it'll be a bit clearer.
You're trying to calculate the total value of all stock on hand, eg
for insurance purposes. That's not 24 bananas, that's $24 of bananas.
And the part you want now is to get the total value of your entire
stock. Great! You're very close to there...
> HERES MY CODE:
>
> for key in prices:
> print prices[key]*stock[key]
>
> ISSUE:
> I need to find a way to add all of those together...any pointers?
... you just need to accumulate a sum. Since this is almost certainly
homework, I won't give you the answer, but here are a few pointers:
* You'll need a single variable (I use the term sloppily, Python
doesn't actually have variables per se) which will collect the final
total.
* Inside your loop, you're currently printing out an int/float with
the value of the current item. Just add it onto your accumulator.
* Python will happily work with integers and floats together, so you
can just do what's obvious and it'll work.
See where that takes you. Have fun! :)
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-05-18 09:22 -0400 |
| Message-ID | <roy-9D8691.09224718052013@news.panix.com> |
| In reply to | #45490 |
In article <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com>,
Bradley Wright <bradley.wright.biz@gmail.com> wrote:
> Confusing subject for a confusing problem (to a novice like me of course!)
> Thx for the help in advance folks
>
> I have (2) dictionaries:
>
> prices = {
> "banana": 4,
> "apple": 2,
> "orange": 1.5,
> "pear": 3
> }
>
> stock = {
> "banana": 6,
> "apple": 0,
> "orange": 32,
> "pear": 15
> }
>
> Here's my instructions:
Hmmm, homework for a class?
> consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!)
I suspect what you're trying to say is that bananas cost BTC 4 each, and
since you've got 6 bananas, you've got BTC 24 worth of bananas, yes?
And now you want to find the total value of your fruit supply?
>> HERES MY CODE:
>
> for key in prices:
> print prices[key]*stock[key]
>
> HERES THE OUTPUT:
>
> 48.0
> 45
> 24
> 0
So far, so good. A couple of things you may have noticed along the way:
1) Your orange unit price was a float, so the total value of all your
oranges is a float as well. That's how math works in Python.
2) The keys are presented in random order. To make the output easier to
interpret, you might want to do:
print key, prices[key]*stock[key]
> ISSUE:
> I need to find a way to add all of those together...any pointers?
The most straight-forward way would be something like:
total = 0
for key in prices:
fruit_subtotal = prices[key]*stock[key]
total += fruit_subtotal
print key, fruit_subtotal
print total
There are better ways to do this in Python, but start like this and get
that to work.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web