0

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

asked Jul 19, 2019 at 19:19

2 Answers 2

1
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 check_table:
 print(x)
 # Getting the number of steaks
 if x[1] == "Classic":
 steaks += 1
 # Getting the style of the steak
 if x[3] == "Rare":
 rare += 1
 elif x[3] == 'Medium':
 medium += 1
 elif x[3] == 'Well-done':
 w_done += 1
 elif x[1] == "Big":
 steaks += 2
 # Getting steak style
 if x[3] == 'Rare':
 rare += 2
 elif x[3] == 'Medium':
 medium += 2
 elif x[3] == 'Well-done':
 w_done += 2
print("# Steaks ", steaks)
print("# Rare ", rare)
print("# Medium ", medium )
print("# Well-done ", w_done)
answered Jul 19, 2019 at 19:30
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man you just fixed my code, yeah I did it too complicated for nothing cheers man, ps sorry for the late reply
1

You should try to turn all those if/elif statements into lookups into some data structure. It will make everything easier to reason about, easier to edit, and it will make you code much more succinct.

For example instead of checking how many steaks with if/elif, just lookup how many steaks are in classic vs big:

steak_count = {
 'Classic': 1,
 'Big': 2
}
count = steak_count['Big'] # 2 steaks for 'Big'

Now if you add a different kind of burger you don't need to write a whole set of if/elif statements again. You can carry this idea to others with something like:

check_table = [(1,'Classic', 'Cheddar', 'Medium'), (2,'Big', 'Cheddar', 'Rare'), (3,'Classic', 'Cheddar', 'Rare')]
steak_count = {
 'classic': 1,
 'big': 2
}
# Variables
counts = {
 'steaks': 0,
 'rare': 0,
 'medium': 0,
 'w_done': 0 
}
for order_num, steak, cheese, cooked in check_table:
 count = steak_count[steak.lower()]
 counts['steaks'] += count
 counts[cooked.lower()] += count
print(counts)
# {'steaks': 4, 'rare': 3, 'medium': 1, 'w_done': 0}
answered Jul 19, 2019 at 19:30

2 Comments

Really thank you Mark, that's a really interesting point of view, I will tested once I get some spare time to carry on the project and will back to you, cheers man
I really like your way of thinking, I wouldn't never think on coding like that really thanks man

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.