【发布时间】:2019-07-19 19:19:01
【问题描述】:
我正在为一家汉堡店用 Python 开发一个应用程序 web,这个应用程序将从数据库中获取一些数据,我需要管理这些数据
从数据库中我会得到一个元组列表,就像这个一样 check_table = [(1,'Classic', 'Cheddar', 'medium'), (2,'Big', 'Cheddar', 'rare'), (3,'Classic', 'Cheddar', 'rare') ]
在这里,我尝试根据牛排的烹饪水平获得适当数量的牛排,如稀有、中等、熟透
我花了很多时间,但我没有让代码正确计数,我什至试图做出类似的陈述
如果 check_table[x][y] == "经典" 和 check_table[x][y] == "稀有":
牛排 += 1 稀有 +=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 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)
我希望得到这样的东西 牛排 4 稀有 3 中等 1 干得好 0
因为经典汉堡包含一块牛排,而大汉堡包含两块牛排,所以我得到了这个,就像 if 语句不存在一样
牛排 4 稀有 0 中 0 干得好 0
希望有人能帮我解决这个问题,谢谢大家
【问题讨论】:
标签: python-3.x for-loop if-statement nested