Re: [Tutor] updating stock list

2018-05-01 Thread Mats Wichmann
On 04/30/2018 04:15 PM, Shannon Evans via Tutor wrote:
> Hi, i'm wanting to update the stock list at the end so that the fruit
> that's given is taken subtracted from the original stock list. The code
> i've written isn't updating it it's just staying the same. Any idea what i
> can do to fix this?
> 

so a few notes in addition to what Alan has already written. COnsider:

if stock[i[1]]>0:

# if it reads awkwardly, look for a better way to write it.
# how quickly would you know what stock[i[1]] is if you looked
# at it a week later?
# instead consider - what is an "i" here? It's a list,
# with a typical entry being
# ["Ronald Crawford", "Bananas"]
# Why not unpack that into named variables right in the loop?

for (person, fruit) in queue:

# now you can refer to things by useful names:

if stock[fruit]>0:
print("Gave {} to {}".format(person,fruit))
# Hint: this is the ideal place to decrement the stock count

does that help?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with loops

2018-05-01 Thread Isaac Tetteh
Hi Shannon,



Yes there is a way. First in my opinion I think queue.json is not really a JSON 
data structure but just a list of list. If you are able to read it then I 
that’s great.



One way to solve this is doing something like below:



queue = [

["James Bruce", "Bananas"],

["Katherine Newton", "Bananas"],

["Deborah Garcia", "Pears"],

["Marguerite Kozlowski", "Pineapples"],

["Kenneth Fitzgerald", "Pineapples"],

["Ronald Crawford", "Bananas"],

["Donald Haar", "Apples"],

["Al Whittenberg", "Bananas"],

["Max Bergevin", "Bananas"],

["Carlos Doby", "Pears"],

["Barry Hayes", "Pineapples"],

["Donald Haar", "Bananas"]

]



stock = {"Apples": 14,

  "Bananas": 14,

  "Pineapples": 0,

  "Pears": 8}



for i in queue:

if stock[i[1]] > 0 :

print("Gave {} to {}".format(i[1],i[0]))

else:

print("Could not give {} to {}".format(i[1],i[0]))



Let us know if you have any other questions.



Isaac,





Sent from Mail for Windows 10




From: Tutor  on behalf of 
Shannon Evans via Tutor 
Sent: Monday, April 30, 2018 8:35:01 AM
To: tutor@python.org
Subject: [Tutor] Help with loops

Hi, is there any way that i can add a loop or iteration or something so
that i dont have to write out every person who has fruit. This information
is coming from the following json files:
*queue.json* file

[

  ["James Bruce", "Bananas"],

  ["Katherine Newton", "Bananas"],

  ["Deborah Garcia", "Pears"],

  ["Marguerite Kozlowski", "Pineapples"],

  ["Kenneth Fitzgerald", "Pineapples"],

  ["Ronald Crawford", "Bananas"],

  ["Donald Haar", "Apples"],

  ["Al Whittenberg", "Bananas"],

  ["Max Bergevin", "Bananas"],

  ["Carlos Doby", "Pears"],

  ["Barry Hayes", "Pineapples"],

  ["Donald Haar", "Bananas"]

]



*stock.json* file

{

"Apples": 14,

"Bananas": 14,

"Pineapples": 0,

"Pears": 8

}

This is what i've done so far:

import json

#Load the stock and queue files
queue=json.load(open("queue.json"))
stock=json.load(open("stock.json"))

if (stock[queue[0][1]]>0):
#in stock
print "Gave Bananas to James Bruce"
else:
print "Could not give Bananas to James Bruce"

if (stock[queue[1][1]]>0):
#in stock
print "Gave Bananas to "+ queue[1][0]
else:
print "Could not give Bananas to "+ queue[1][0]

if (stock[queue[2][1]]>0):
#in stock
print "Gave Pears to "+ queue[2][0]
else:
print "Could not give Pears to "+ queue[2][0]

if (stock[queue[3][1]]>0):
#in stock
print "Gave Pineapples to "+ queue[3][0]
else:
print "Could not give Pineapples to "+ queue[3][0]

Thanks
Shannon
___
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