简体   繁体   English

Python 3.4:尝试获取此模块以汇总此二维数组中的销售总额。 不工作

[英]Python 3.4 : Trying to get this module to sum the sale totals in this 2d array. Not working

def sales_per_zip(sales):
    sales_sum = 0
    i = 1
    print(sales[1][0])
    zipcode = input("Enter the Zip Code here:")
    if zipcode == sales[1][0]:
        while i <= 6:
            sales_sum =+ sales[1][i]
            i =+ 1
            print(i)

    print(sales_sum)

sales = [
["Zip Code", "Mocha", "Latte", "Regular", "Decaf", "Caramel"],
[48093,180,100,200,180,150],
[48088,270,330,160,150,250],
[48026,240,310,450,100,320],
[48066,200,230,350,110,360],
]
opt = ""

print("What would you like to do?")
print("Z = Get Total Sales by Zip Code.")
print("C = Get Total Sales by Coffee Type.")
print("G = Get Grand Total of all Coffee Sales.")
print("HZ = Get Highest Sales by Zip Code.")
print("HC = Get Highest Sales by Coffee Type.")
opt = str(input("Enter your option here:"))

if opt == "z" or opt == "Z":
    sales_per_zip(sales)
#elif opt == "c" or opt == "C":
#
#elif opt == "g" or opt == "G":
#
#elif opt == "hz" or opt == "HZ":
#
#elif opt == "hc" or opt == "HC":

This is what i have so far. 这是我到目前为止所拥有的。 The first def is not summing the sales correctly. 第一定义未正确汇总销售量。 The output is just a 0 I can't seem to see why. 输出只是一个0,我似乎看不出为什么。 I'm currently in my first python/programming class. 我目前在我的第一个python / programming类中。 I have this running in raptor but transferring it to python is becoming quite the task... 我已经在猛禽中运行了,但是将其转移到python变得非常重要...

You have some incorrect operators =+ instead of += . 您有一些不正确的运算符=+而不是+= Also, you need to parse the input zipcode as a int. 另外,您需要将输入的邮政编码解析为一个整型。 Lastly, the while loop should have < instead of <= so you don't get an IndexError. 最后,while循环应使用<而不是<=这样就不会出现IndexError。

def sales_per_zip(sales):
    sales_sum = 0
    i = 1
    print(sales[1][0])
    zipcode = int(input("Enter the Zip Code here:"))
    if zipcode == sales[1][0]:
        while i < len(sales[1]):
            sales_sum += sales[1][i]
            i += 1
            print(i)

    print(sales_sum)

Additionaly, you could add another function to make sure you get a valid input zipcode. 另外,您可以添加另一个功能以确保获得有效的输入邮政编码。

def get_zipcode():
    try:
        zipcode = int(input("Enter the Zip Code here:"))
        return zipcode
    except:
        print("ERROR: zipcode must be an integer")
        return get_zipcode()

def sales_per_zip(sales):
    sales_sum = 0
    i = 1
    print(sales[1][0])
    zipcode = get_zipcode()
    if zipcode == sales[1][0]:
        while i < len(sales[1]):
            sales_sum += sales[1][i]
            i += 1
            print(i)

    print(sales_sum)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM