[Tutor] Help with loops

Isaac Tetteh itetteh34 at hotmail.com
Mon Apr 30 12:03:52 EDT 2018


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<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10



________________________________
From: Tutor <tutor-bounces+itetteh34=hotmail.com at python.org> on behalf of Shannon Evans via Tutor <tutor at python.org>
Sent: Monday, April 30, 2018 8:35:01 AM
To: tutor at 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 at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list