[Tutor] Help with loops

Shannon Evans shannonev101 at googlemail.com
Mon Apr 30 09:35:01 EDT 2018


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


More information about the Tutor mailing list