【问题标题】:Nested if into nested for in python嵌套if进入python中的嵌套for
【发布时间】: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


    【解决方案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)
    

    【讨论】:

    • 谢谢你刚刚修复了我的代码,是的,我做的太复杂了,没有任何欢呼,抱歉,回复晚了
    【解决方案2】:

    您应该尝试将所有这些if/elif 语句转换为对某些数据结构的查找。它会让一切更容易推理,更容易编辑,它会让你的代码更加简洁。

    例如,不要使用 if/elif 检查有多少牛排,而只需查找经典与大牛排有多少:

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

    现在,如果您添加不同种类的汉堡,您无需再次编写一整套 if/elif 语句。您可以通过以下方式将这个想法传达给其他人:

    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}
    

    【讨论】:

    • 真的非常感谢马克,这是一个非常有趣的观点,一旦我有空闲时间继续这个项目,我会测试并回复你,干杯人
    • 我真的很喜欢你的思维方式,我永远不会想到这样的编码真的谢谢人
    猜你喜欢
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2020-10-08
    • 2015-10-18
    • 1970-01-01
    相关资源
    最近更新 更多