vendredi 19 juillet 2019

Nested if into nested for in python

I'm developing an application web in Python for a Burger shop, This app will get some data from a Data base and I need to manage this data

From the Data base I will get a list of tuples just like this one check_table = [(1,'Classic', 'Cheddar', 'medium'), (2,'Big', 'Cheddar', 'rare'), (3,'Classic', 'Cheddar', 'rare')]

In here I trying to get the proper number of steaks according to their cooking level, Like rare, medium, Well-done

I spend a lot of time but I don't get the code to count properly, I even tried to make statements like

if check_table[x][y] == "Classic" and check_table[x][y] == "Rare":

steak += 1 rare +=1

Here's the code:

check_table = [(1,'Classic', 'Cheddar', 'Medium'), (2,'Big', 'Cheddar', 'Rare'), (3,'Classic', 'Cheddar', 'Rare')]

# Variables
steaks = 0
rare = 0
medium = 0
w_done = 0

# Getting all the data from a nested for
for x in range(len(check_table)):
    for y in range(len(check_table[0])):
        # Getting the number of steaks
        if check_table[x][y] == "Classic":
            steaks += 1
            # Getting the style of the steak
            if check_table[x][y] == "Rare":
                rare += 1
            elif check_table[x][y] == 'Medium':
                medium += 1
            elif check_table[x][y] == 'Well-done':
                w_done += 1

        elif check_table[x][y] == "Big":
            steaks += 2
            # Getting steak style
            if check_table[x][y] == 'Rare':
                rare += 2
            elif check_table[x][y] == 'Medium':
                medium += 2
            elif check_table[x][y] == 'Well-done':
                w_done += 2

print("# Steaks ", steaks)
print("# Rare ", rare)
print("# Medium ", medium )
print("# Well-done ", w_done)

I expected to get something like this Steaks 4 rare 3 medium 1 Well-done 0

Because a classic burger contains one steak meanwhile the big one contains two steaks, instead I get this, like if the if statements were not there

Steaks 4 Rare 0 Medium 0 Well-done 0

Hope someone can help me with this one, Thank you guys

Aucun commentaire:

Enregistrer un commentaire