Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #84035 > unrolled thread
| Started by | Luke Tomaneng <luketomaneng@gmail.com> |
|---|---|
| First post | 2015-01-19 16:12 -0800 |
| Last post | 2015-01-21 09:59 +0100 |
| Articles | 11 — 6 participants |
Back to article view | Back to comp.lang.python
Concerning Dictionaries and += in Python 2.x Luke Tomaneng <luketomaneng@gmail.com> - 2015-01-19 16:12 -0800
Re: Concerning Dictionaries and += in Python 2.x Chris Angelico <rosuav@gmail.com> - 2015-01-20 11:21 +1100
Re: Concerning Dictionaries and += in Python 2.x Luke Tomaneng <luketomaneng@gmail.com> - 2015-01-19 16:31 -0800
Re: Concerning Dictionaries and += in Python 2.x MRAB <python@mrabarnett.plus.com> - 2015-01-20 00:27 +0000
Re: Concerning Dictionaries and += in Python 2.x Luke Tomaneng <luketomaneng@gmail.com> - 2015-01-19 16:34 -0800
Re: Concerning Dictionaries and += in Python 2.x Chris Angelico <rosuav@gmail.com> - 2015-01-20 11:46 +1100
Re: Concerning Dictionaries and += in Python 2.x Dan Stromberg <drsalists@gmail.com> - 2015-01-19 16:54 -0800
Re: Concerning Dictionaries and += in Python 2.x Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-21 05:01 +0000
Re: Concerning Dictionaries and += in Python 2.x Peter Otten <__peter__@web.de> - 2015-01-21 09:43 +0100
Re: Concerning Dictionaries and += in Python 2.x Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-21 14:10 +0000
Re: Concerning Dictionaries and += in Python 2.x Peter Otten <__peter__@web.de> - 2015-01-21 09:59 +0100
| From | Luke Tomaneng <luketomaneng@gmail.com> |
|---|---|
| Date | 2015-01-19 16:12 -0800 |
| Subject | Concerning Dictionaries and += in Python 2.x |
| Message-ID | <bc9e302a-356c-4223-9c25-6a8003db48e6@googlegroups.com> |
I have been having a bit of trouble with the things mentioned in the title. I have written the following script for a Codecademy course:
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total += prices[item]
stock[item] = stock[item] - 1
return total
Whenever I run this script, "4" is returned. It does not seem to matter what in in the list the script is run on. I have tried this on the Codecademy interpreter/emulator (I'm not sure which they use) and the repl.it interpreter, but for the same result. If anyone could find the glitch in my code, please let me know. Thanks!
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-01-20 11:21 +1100 |
| Message-ID | <mailman.17869.1421713307.18130.python-list@python.org> |
| In reply to | #84035 |
On Tue, Jan 20, 2015 at 11:12 AM, Luke Tomaneng <luketomaneng@gmail.com> wrote: > def compute_bill(food): > total = 0 > for item in food: > if stock[item] > 0: > total += prices[item] > stock[item] = stock[item] - 1 > return total > Whenever I run this script, "4" is returned. It does not seem to matter what in in the list the script is run on. I have tried this on the Codecademy interpreter/emulator (I'm not sure which they use) and the repl.it interpreter, but for the same result. If anyone could find the glitch in my code, please let me know. Thanks! > In Python, indentation determines block structure. Have another look at this function, and see if you can figure out where the problem is; hint: try printing something out every time you claim a piece of stock. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Luke Tomaneng <luketomaneng@gmail.com> |
|---|---|
| Date | 2015-01-19 16:31 -0800 |
| Message-ID | <78ab68ee-d00c-4c0e-9e86-ebbbc06e4452@googlegroups.com> |
| In reply to | #84037 |
On Monday, January 19, 2015 at 4:21:58 PM UTC-8, Chris Angelico wrote: > On Tue, Jan 20, 2015 at 11:12 AM, Luke Tomaneng wrote: > > def compute_bill(food): > > total = 0 > > for item in food: > > if stock[item] > 0: > > total += prices[item] > > stock[item] = stock[item] - 1 > > return total > > Whenever I run this script, "4" is returned. It does not seem to matter what in in the list the script is run on. I have tried this on the Codecademy interpreter/emulator (I'm not sure which they use) and the repl.it interpreter, but for the same result. If anyone could find the glitch in my code, please let me know. Thanks! > > > > In Python, indentation determines block structure. Have another look > at this function, and see if you can figure out where the problem is; > hint: try printing something out every time you claim a piece of > stock. > > ChrisA Hm. I fixed the indentation like you said, and it worked fine. The only reason I changed the indentation to what you saw in the first place is because Codecademy's Python engine registered an error. However, repl.it accepted the script. I have concluded that the original mistake was actually on the Codecademy site instead of my script. Thanks for helping me out.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-01-20 00:27 +0000 |
| Message-ID | <mailman.17871.1421713863.18130.python-list@python.org> |
| In reply to | #84035 |
On 2015-01-20 00:12, Luke Tomaneng wrote:
> I have been having a bit of trouble with the things mentioned in the title. I have written the following script for a Codecademy course:
> stock = {
> "banana": 6,
> "apple": 0,
> "orange": 32,
> "pear": 15
> }
>
> prices = {
> "banana": 4,
> "apple": 2,
> "orange": 1.5,
> "pear": 3
> }
>
> def compute_bill(food):
> total = 0
> for item in food:
> if stock[item] > 0:
> total += prices[item]
> stock[item] = stock[item] - 1
> return total
> Whenever I run this script, "4" is returned. It does not seem to matter what in in the list the script is run on. I have tried this on the Codecademy interpreter/emulator (I'm not sure which they use) and the repl.it interpreter, but for the same result. If anyone could find the glitch in my code, please let me know. Thanks!
>
Work through it a step at a time.
You haven't said what 'food' is when 'compute_bill is called, but I'm
guessing that the first item it checks is "banana".
stock["banana"] == 6, so it adds prices["banana"] to total, subtracts 1
from stock["banana"], and then returns the total, 4, because that's
what you've told it to do.
[toc] | [prev] | [next] | [standalone]
| From | Luke Tomaneng <luketomaneng@gmail.com> |
|---|---|
| Date | 2015-01-19 16:34 -0800 |
| Message-ID | <78c7d3ea-0bff-4716-acc9-1ff38d52dfe2@googlegroups.com> |
| In reply to | #84035 |
Thanks Chris / Mr. Angelico / whatever you prefer. I attempted to post a reply to you before but it could not be viewed even after refreshing several times. You've been helpful.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-01-20 11:46 +1100 |
| Message-ID | <mailman.17873.1421714793.18130.python-list@python.org> |
| In reply to | #84042 |
On Tue, Jan 20, 2015 at 11:34 AM, Luke Tomaneng <luketomaneng@gmail.com> wrote: > Thanks Chris / Mr. Angelico / whatever you prefer. I attempted to post a reply to you before but it could not be viewed even after refreshing several times. You've been helpful. > My pleasure! Your earlier email did come through; sometimes there are short delays, it's not a big deal. As to a mode of address, most people use first names around here, so "Chris" is common. But when lots of Chrises get to talking, that can get ambiguous, so I might be referred to as ChrisA, or as Rosuav (my email address). But Mr Angelico is fine, if a little formal :) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| Date | 2015-01-19 16:54 -0800 |
| Message-ID | <mailman.17874.1421715268.18130.python-list@python.org> |
| In reply to | #84035 |
On Mon, Jan 19, 2015 at 4:12 PM, Luke Tomaneng <luketomaneng@gmail.com> wrote:
> I have been having a bit of trouble with the things mentioned in the title. I have written the following script for a Codecademy course:
> stock = {
> "banana": 6,
> "apple": 0,
> "orange": 32,
> "pear": 15
> }
>
> prices = {
> "banana": 4,
> "apple": 2,
> "orange": 1.5,
> "pear": 3
> }
>
> def compute_bill(food):
> total = 0
> for item in food:
> if stock[item] > 0:
> total += prices[item]
> stock[item] = stock[item] - 1
> return total
> Whenever I run this script, "4" is returned. It does not seem to matter what in in the list the script is run on. I have tried this on the Codecademy interpreter/emulator (I'm not sure which they use) and the repl.it interpreter, but for the same result. If anyone could find the glitch in my code, please let me know. Thanks!
You're returning total inappropriately - the first time stock[item] is > 0.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-01-21 05:01 +0000 |
| Message-ID | <m9nbrj$i7h$1@dont-email.me> |
| In reply to | #84035 |
On Mon, 19 Jan 2015 16:12:57 -0800, Luke Tomaneng wrote: > I have been having a bit of trouble with the things mentioned in the > title. I've uploaded a slightly different approach to your code at: http://www.sined.co.uk/tmp/shop.py.txt -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-01-21 09:43 +0100 |
| Message-ID | <mailman.17908.1421829827.18130.python-list@python.org> |
| In reply to | #84105 |
Denis McMahon wrote:
> On Mon, 19 Jan 2015 16:12:57 -0800, Luke Tomaneng wrote:
>
>> I have been having a bit of trouble with the things mentioned in the
>> title.
>
> I've uploaded a slightly different approach to your code at:
>
> http://www.sined.co.uk/tmp/shop.py.txt
>
> def compute_bill(shopping):
> """
> Takes a dictionary of purchases requested in the form {item: quantity}
> Returns a tuple of:
> a dictionary of items supplied in the form {item: quantity}; and
> the cost of the supplied items
> """
> # the invoice amount
> invoice = 0
> # what we were able to supply
> sold = {k:0 for k in shopping.keys()}
There is also dict.from_keys()
> # check each requested item
> for item in shopping:
>
> # try and sell the requested qty
> for i in range(shopping[item]):
The inner loop is not just inefficient for stock sold in large quantities,
it will fail for stock sold by weight, volume etc.
> # if we have stock remaining
> if stock[item] > 0:
>
> # reduce stock count for item by 1
> stock[item] = stock[item] - 1
> # add 1 item to the sale
> sold[item] += 1
> # add item cost to the invoice
> invoice += prices[item]
>
> # return the items supplied and their cost
> return sold, invoice
Here is a possible alternative:
sold = {}
for item, wanted_quantity in shopping.iteritems(): # items() in Python 3
available_quantity = stock.get(item, 0)
sold_quantity = min(wanted_quantity, available_quantity)
sold[item] = sold_quantity
stock[item] -= sold_quantity
invoice += sold_quantity * prices[item]
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-01-21 14:10 +0000 |
| Message-ID | <m9oc18$t3b$1@dont-email.me> |
| In reply to | #84110 |
On Wed, 21 Jan 2015 09:43:31 +0100, Peter Otten wrote: > There is also dict.from_keys() See, I learned something too. > The inner loop is not just inefficient for stock sold in large > quantities, Agreed, but as for: > it will fail for stock sold by weight, volume etc. I was trying to stay true to OPs original code and not introduce [too many] additional complications to his learning exercise. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-01-21 09:59 +0100 |
| Message-ID | <mailman.17909.1421830806.18130.python-list@python.org> |
| In reply to | #84105 |
Peter Otten wrote:
> Denis McMahon wrote:
>> sold = {k:0 for k in shopping.keys()}
>
> There is also dict.from_keys()
Sorry, fromkeys():
>>> shopping = {'orange': 5, 'pear': 5, 'banana': 5, 'apple': 4}
>>> dict.fromkeys(shopping, 0)
{'banana': 0, 'orange': 0, 'apple': 0, 'pear': 0}
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web