[英]sum of squares (4 values shaping a square) within a 2d numpy array. Python
[英]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":
这是我到目前为止所拥有的。 第一定义未正确汇总销售量。 输出只是一个0,我似乎看不出为什么。 我目前在我的第一个python / programming类中。 我已经在猛禽中运行了,但是将其转移到python变得非常重要...
您有一些不正确的运算符=+
而不是+=
。 另外,您需要将输入的邮政编码解析为一个整型。 最后,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)
另外,您可以添加另一个功能以确保获得有效的输入邮政编码。
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.