如何求和循环内的值?

How to sum up the value inside the loop?

下面是我的代码,我想知道如何在循环中断后将所有价格相加。如您所见,它将循环直到输入无效。每个组合都有不同的价格,如果我订购 3 个不同的组合,价格也会不同。我可以总结所有价格的代码是多少?

combo = str(input('Enter Combo Type (A/B/C or X to end) ='))

while combo != 'x' and 'X':

    if combo == 'a' and 'A':
        qtt = int(input('How many do you want to order  ='))
        price = qtt * 40
        print('Order price for combo A =', price)
        combo = str(input('Enter Combo Type (A/B/C or X to end) ='))

    elif combo == 'b' and 'B':
        qtt = int(input('How many do you want to order  ='))
        price = qtt * 52
        print('Order price for combo B =', price)
        combo = str(input('Enter Combo Type (A/B/C or X to end) ='))

    elif combo == 'c' and 'C':
        qtt = int(input('How many do you want to order  ='))
        price = qtt * 46
        print('Order price for combo C =', price)
        combo = str(input('Enter Combo Type (A/B/C or X to end) ='))

    elif combo == 'x' and 'X':
        break

    else:
        print("Wrong input, please try again.")

您需要将总价附加到列表中,然后在最后对其求和。

关于您的代码的一些事情:

  1. 不要使用像 if combo == "x" and "X" 这样的条件,你应该使用 if combo == "x" or combo == "X",或者更简单地说,如果你需要检查是否可以使用 if combo in ('x', 'X')是多个值。

  2. re: 条件,您可以考虑检查 combo.lower(),即 if combo.lower() == "x" 而不是检查多个字符。您甚至可以在 input 调用时调用 .lower().upper(),即 combo = str(input("Prompt:")).lower()

  3. 您可以简化代码,同时将 combo 输入调用放在 while 循环的顶部,然后检查您的条件。与您的价格和数量输入相同——写一个加载您已经设置的输入的打印语句。这将大大缩短和清理您的代码。

  4. 现在是研究 f 字符串而不是串联的好时机。使用 f-strings,您可以编写如下内容:f'Order price for combo {combo} = {str(price)}' 而不是乱用 + 符号

    combo = None
    combo_list = []
    
    while combo != 'x':
        combo = str(input('Enter Combo Type (A/B/C or X to end) = ')).lower()
        if combo == 'x':
            break
    
        qtt = int(input('How many do you want to order = '))
        if combo == 'a':
            price = qtt * 40
        elif combo == 'b':
            price = qtt * 52
        elif combo == 'c':
            price = qtt * 46
        else:
            print("Wrong input, please try again.")
    
        combo_list.append(price)
        print(f'Order price for combo {combo} = {str(price)}')
    
    print(f'Total price = {str(sum(combo_list))}')
    

保留每个选择的 运行 总价并在最后打印出来。

关于此代码的一些其他注释:

  1. 您可以通过将 input 行放在循环的开头来避免重复。这种重复提示的一个非常标准的模式是在 while True 上循环,获取输入,并在匹配终止条件时 break
  2. 您可以通过将每个组合的价格放入字典中来避免重复计算价格。
  3. if combo = 'a' and 'A' 与多个值进行比较是行不通的 - 仅 .upper() 将值强制为大写并与之进行比较更简单。
total = 0
prices = {
    'A': 40,
    'B': 52,
    'C': 46,
}

while True:
    combo = input('Enter Combo Type (A/B/C or X to end) =').upper()
    if combo == 'X':
        break
    if combo not in prices:
        print("Wrong input, please try again.")
        continue

    price = int(input('How many do you want to order  =')) * prices[combo]
    print(f'Order price for combo {combo} = {price}')
    total += price

print(f'Total: {total}')

您可以对每个回复进行计数。例如:

count = {'a':0, 'b':0, 'c':0}

qtt = int(input('How many of A do you want to order = '))
count['a'] += qtt

退出循环后:

total = count['a'] * 40 + count['b'] * 52 + count['c'] * 46