Python 3.4:试图让这个模块对这个2d数组中的销售总数求和。不工作
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. I'm currently in my first python/programming class. I have this running in raptor but transferring it to python is becoming quite the task...
这就是我到目前为止所拥有的。第一个def没有正确地总结销售额。输出只是一个0我似乎无法理解为什么。我目前正处于我的第一个python /编程课程中。我有这个在猛禽运行,但转移到python正在成为一项任务......
1 个解决方案
#1
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.
你有一些不正确的运算符= +而不是+ =。此外,您需要将输入的zipcode解析为int。最后,while循环应该有 <而不是<=所以你不会得到一个indexerror。< p>
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)
更多相关文章
- 字体图标的引入和通过媒体查询改变导航样式
- HTML样式和常用选择器
- 字体图标的引用和自定义样式/媒体查询的使用
- 数据库的CURD操作、PDO本质与原理的学习
- CSS之伪类选择器和简单盒子简单案例
- 伪类选择器与盒模型常用属性
- 伪类选择器-结构伪类、根据位置选择匹配
- 7.4——常用标签与应用场景之表格与单元格
- css伪类选择器和盒模型