4

My question is how to print out something if nothing matched in for loop. For example:

a = {'store' : 'A', 'menu' : 'pizza', 'price' : 20000}
b = {'store' : 'B', 'menu' : 'chicken', 'price' : 18000}
c = {'store' : 'C', 'menu' : 'noodle', 'price' : 5000}
d = {'store' : 'D', 'menu' : 'sushi', 'price' : 15000}
e = {'store' : 'E', 'menu' : 'chicken', 'price' : 23000}
f = {'store' : 'F', 'menu' : 'pork', 'price' : 30000}

Total = [a, b, c, d, e, f]

l = input('what food?:  ')
p = int(input('how much you want to spend?  '))

for i in range(5):
    if Total[i]['menu'] == l and int(Total[i]['price']) <= p:
        print('menu', Total[i]['store'], 'price', Total[i]['price'])

after the loop, if there is no such store satisfying conditions, I want to print out 'There is no store'.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • 1
    keep a bool somewhere like found = False, then in your if set it to True, and outside if you can check if its still False, means not found – pacukluka May 19 '20 at 02:40
  • If the same food appears on multiple menus, do you want to print it twice or just once? – Daniel Walker May 19 '20 at 02:44

3 Answers3

3

If you are only interested in the first restaurant that will match the condition, you can use a lesser-known Python construct:

for i in range(5):
    if Total[i]['menu'] == l and int(Total[i]['price']) <= p:
        print('menu', Total[i]['store'], 'price', Total[i]['price'])
        break
else:
    print("Sorry, there are no matching restaurants.")
Selcuk
  • 57,004
  • 12
  • 102
  • 110
3

Just add a boolean to check if a price was found.

a = {'store' : 'A', 'menu' : 'pizza', 'price' : 20000}
b = {'store' : 'B', 'menu' : 'chicken', 'price' : 18000}
c = {'store' : 'C', 'menu' : 'noodle', 'price' : 5000}
d = {'store' : 'D', 'menu' : 'sushi', 'price' : 15000}
e = {'store' : 'E', 'menu' : 'chicken', 'price' : 23000}
f = {'store' : 'F', 'menu' : 'pork', 'price' : 30000}

Total = [a, b, c, d, e, f]

l = input('what food?:  ')
p = int(input('how much you want to spend?  '))

found = False
for i in range(5):
    if Total[i]['menu'] == l and int(Total[i]['price']) <= p:
        print('menu', Total[i]['store'], 'price', Total[i]['price'])
        found =  True

if not found:
    print ("No stores were found.")
0

I would transform your dictionary variables into a nested dictionary, where menu is the key:

a = {"store": "A", "menu": "pizza", "price": 20000}
b = {"store": "B", "menu": "chicken", "price": 18000}
c = {"store": "C", "menu": "noodle", "price": 5000}
d = {"store": "D", "menu": "sushi", "price": 15000}
e = {"store": "E", "menu": "chicken", "price": 23000}
f = {"store": "F", "menu": "pork", "price": 30000}

Total = [a, b, c, d, e, f]

restructured = {d.get("menu"): {k: v for k, v in d.items() if k != "menu"} for d in Total}

Which will give you this structure:

{'pizza': {'store': 'A', 'price': 20000}, 'chicken': {'store': 'E', 'price': 23000}, 'noodle': {'store': 'C', 'price': 5000}, 'sushi': {'store': 'D', 'price': 15000}, 'pork': {'store': 'F', 'price': 30000}}

We could avoid this restructuring by not having separate variables to begin with, and going for a nested dictionary instead. You also don't need to loop over dictionaries(linear search), which is much faster for repetitive lookups(constant time).

You can then simply access this dictionary by menu item, and check if you can buy the menu item or not:

food = input("what food?:  ")
budget = int(input("how much you want to spend?:  "))

menu = restructured.get(food)
if menu:
    price = int(menu.get("price"))
    if budget <= price:
        store = menu.get("store")
        print(f"Store: {store}, Menu: {food}, Price: {price}")
    else:
        print(f"{food} costs too much!")
else:
    print(f"{food} not found in menu!")

I've kept the above code relatively simple, but there are more elegant ways to do this.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75