Coding help

This forum is a place to ask questions about the python programming language.
Post Reply
Emre
New User
Posts: 3
Joined: 22 May 2018, 08:34

Coding help

Post by Emre » 13 Sep 2018, 15:39

Hello Mr. Mackenty,

Here is the problem I am currently struggling with. I would like to only search for the first sub-elements in each element in my two-dimensional array. To put it in context, I present the stock of a supermarket as a two-dimensional array, where each element consists of three sub-elements, which are the name, quantity and the UPC code of the item. When the user wishes to search for an item by its name, the program should compare the input with the first sub-elements in each element and stop when it finds a match. Finally, it should print the full element, stating the items name, quantity, and the UPC code. I would very much appreciate if you could help me with this.
1 x

Tags:

User avatar
Mr. MacKenty
Site Admin
Posts: 88
Joined: 28 Apr 2018, 17:06
Answers: 3

Re: Coding help

Post by Mr. MacKenty » 13 Sep 2018, 20:00

Emre wrote:
13 Sep 2018, 15:39
Hello Mr. Mackenty,

Here is the problem I am currently struggling with. I would like to only search for the first sub-elements in each element in my two-dimensional array. To put it in context, I present the stock of a supermarket as a two-dimensional array, where each element consists of three sub-elements, which are the name, quantity and the UPC code of the item. When the user wishes to search for an item by its name, the program should compare the input with the first sub-elements in each element and stop when it finds a match. Finally, it should print the full element, stating the items name, quantity, and the UPC code. I would very much appreciate if you could help me with this.
Emre!

I love this question. Next time, please paste in some code. :-)

Code: Select all



inventory = [["apple",12,112345],["pear",100,76628],["frozen pizza",10,99878]]


for i in inventory:
    print(i)
#
# the code above outputs: 
#
# ['apple', 12, 112345]
# ['pear', 100, 76628]
# ['frozen pizza', 10, 99878]
#

# If I want to look only at the first element, I would the code below: 

for i in inventory:
    print(i[0])
#
# the code above outputs: 
#
# apple
# pear
# frozen pizza

# So then, if we combine all this, we could do something like: 

search_term = input("Please type the name of the item you want: ")
counter = 0
for i in inventory:
    if search_term == i[0]:
        print("======== Emre's Store Inventory System =========")
        print("Name: ", i[0])
        print("Inventory: ", i[1])
        print("UPC code: ", i[2])
        print("================================================")
        break
    else:
        counter = counter +1
        if counter == len(inventory):
            print("I'm sorry I couldn't find that item.")

Please do write back and tell me if this works?
0 x

Post Reply