[Tutor] real time response
hi all: I have a code that takes about 20 seconds to complete, but I also need to response to events (the appearance of red dots on a canvas) so I could place a few lines to check for this condition like this def do_stuff: blah blah check() blah blah blah check() blah blah blah check() blah and then either break or return if condition is met? But that just isn't responsive enough. Is there a way to make function check for the event every 0.5 seconds without interfering with the do_stuff, ie a code that has its' own space? then something interrupts and closes down do_stuff and run another function in response? thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looping through Dictionaries
On 05/23/2017 11:38 AM, Rafael Knuth wrote: > I wrote a function (shopping list) which calculates the total price of > the purchase (quantity * price) as well as the stock change (stock - > quantity). I know the latter is imperfect (my function does not take > into account if items are out of stock for example, but that's my next > challenge. The function does exactly what it's supposed to do: > > def shopping(quantity, price, stock): > total = 0 > for key in quantity: > total += quantity[key] * price[key] > stock_update = stock[key] - quantity[key] > print(stock_update) > print(total) > > quantity = { > "bapple": 10, > "banana": 20 > } > > price = { > "bapple": 5, > "banana": 3 > } > > stock = { > "bapple": 20, > "banana": 50 > } > > shopping(quantity, price, stock) > > Now, I want to print the item next to the stock update, and I am > receiving an error message. I couldn't figure out how to fix that: in the code below you're trying to use "value" as your key, but it isn't the key. Why did you change that from the code above? it helps if you include the errors, by the way :) > > def shopping(quantity, price, stock): > total = 0 > for key, value in quantity.items(): > total += quantity[value] * price[value] > stock_update = stock[value] - quantity[value] > print(key, stock_update) > print(total) > > quantity = { > "bapple": 10, > "banana": 20 > } > > price = { > "bapple": 5, > "banana": 3 > } > > stock = { > "bapple": 20, > "banana": 30 > } > > shopping(quantity, price, stock) > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looping through Dictionaries
On 23/05/17 18:38, Rafael Knuth wrote: > Now, I want to print the item next to the stock update, and I am > receiving an error message. I couldn't figure out how to fix that: So show us the error message, all of it. I can't begin to guess what it might be. UI also don't understand what "print the item next to the stock update" means. Do you mean the actual location of the data in the printed string? Or do you mean the value(which?) next to the one in your function? Can you show us some examples? > def shopping(quantity, price, stock): > total = 0 > for key, value in quantity.items(): > total += quantity[value] * price[value] > stock_update = stock[value] - quantity[value] I don't see how this would work. value is the value from the dictionary which is an integer. But you are using it as a key which should be a string? So in your first iteration key,value will be key = 'bapple', value=10 so you are trying to evaluate stock[10]-quantity[10] but thee are no such items. I suspect you mean: stock['bapple'] - quantity['bapple'] or in your function: stock[key] - quantity[key] But in that case you don;t need the value from items() you can just iterate over the keys: for item in quantity: ... stock_update = stock[item] - quantity[item] print(item, stock_update) > print(key, stock_update) > print(total) > > quantity = { > "bapple": 10, > "banana": 20 > } > > price = { > "bapple": 5, > "banana": 3 > } > > stock = { > "bapple": 20, > "banana": 30 > } > > shopping(quantity, price, stock) I'm not clear on what these variables indicate. What is the difference between quantity and stock? Also this might be a good time to look at classes and objects. You could avoid all the multiple stores by putting the various data elements in a class. This would avoid the potential headaches caused by mis-typing the item names in each store, for example. You could then have variables bapple and banana and access, for example, bapple.quantity and bapple.price. And just keep a simple list of items. Just a thought... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Looping through Dictionaries
I wrote a function (shopping list) which calculates the total price of the purchase (quantity * price) as well as the stock change (stock - quantity). I know the latter is imperfect (my function does not take into account if items are out of stock for example, but that's my next challenge. The function does exactly what it's supposed to do: def shopping(quantity, price, stock): total = 0 for key in quantity: total += quantity[key] * price[key] stock_update = stock[key] - quantity[key] print(stock_update) print(total) quantity = { "bapple": 10, "banana": 20 } price = { "bapple": 5, "banana": 3 } stock = { "bapple": 20, "banana": 50 } shopping(quantity, price, stock) Now, I want to print the item next to the stock update, and I am receiving an error message. I couldn't figure out how to fix that: def shopping(quantity, price, stock): total = 0 for key, value in quantity.items(): total += quantity[value] * price[value] stock_update = stock[value] - quantity[value] print(key, stock_update) print(total) quantity = { "bapple": 10, "banana": 20 } price = { "bapple": 5, "banana": 3 } stock = { "bapple": 20, "banana": 30 } shopping(quantity, price, stock) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursion
Alan Gauld via Tutor wrote: > On 23/05/17 06:18, Peter Otten wrote: >> Michael C wrote: >> >>> oh ya, my function does in fact take no input and doesn't change >>> anything, and all i wanted to was to call itself a 2nd time, yes, so I >>> solved it a few hours back ,and it's good enough for me for now :) >> >> Would you mind showing the code? I'd like to see how you avoid infinite >> recursion. > > I suspect he is not using recursion, just calling his function twice. I was hoping for something creative ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursion
On 23/05/17 06:18, Peter Otten wrote: > Michael C wrote: > >> oh ya, my function does in fact take no input and doesn't change anything, >> and all i wanted to was to call itself a 2nd time, yes, so I solved it a >> few hours back ,and it's good enough for me for now :) > > Would you mind showing the code? I'd like to see how you avoid infinite > recursion. I suspect he is not using recursion, just calling his function twice. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursion
no i don't have a way, it just hasn't happened yet LOL On Mon, May 22, 2017 at 10:18 PM, Peter Otten <__pete...@web.de> wrote: > Michael C wrote: > > > oh ya, my function does in fact take no input and doesn't change > anything, > > and all i wanted to was to call itself a 2nd time, yes, so I solved it a > > few hours back ,and it's good enough for me for now :) > > Would you mind showing the code? I'd like to see how you avoid infinite > recursion. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor