我正在编写一个计算平均值的程序。一切正常,除非程序尝试计算平均值。似乎没有存储我的输入,因此它计算平均值为0.0。我在3.6上使用Python IDLE。这是我的代码:
def main():
#variables holding the total number of wins per year
year1 = 0
year2 = 0
year3 = 0
year4 = 0
year5 = 0
average = year1 + year2 + year3 + year4 + year5/5
#Introduction
print ("Enter number of wins when prompted")
#Ask user for win totals per year
year1 = (input("Please enter the number of wins for year one: "))
year2 = (input("Please enter the number of wins for year one: "))
year3 = (input("Please enter the number of wins for year one: "))
year4 = (input("Please enter the number of wins for year one: "))
year5 = (input("Please enter the number of wins for year one: "))
#calculate average
print ("Total average of wins over five years: ", average)
这是输入我的值后的结果:
在提示时输入获胜次数
请输入第一年的获胜次数:8
请输入第一年的获胜次数:6
请输入第一年的获胜次数:3
请输入第一年的获胜次数:7
请输入第一年的获胜次数:9
五年总胜率:0.0
答案 0 :(得分:2)
Daniel的更新是正确的,您创建平均值的时间太早了。 以下是一些更新的代码,可帮助您学习:
print ("Enter number of wins when prompted")
#Ask user for win totals per year
wins_by_year = []
for year in range(5):
wins_by_year.append(int(input("Please enter the number of wins for year {}: ".format(year+1))))
average = sum(wins_by_year)/len(wins_by_year)
print ("Total average of wins over five years: {}".format(average))
答案 1 :(得分:0)
您必须在分配变量之后计算平均值,还需要将输入转换为整数(int)。另外,正如@Denziloe和@Vage Egiazarian指出的那样,您应该考虑算术运算的顺序。请遵循代码:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'listFilter'
})
export class ListFilterPipe implements PipeTransform {
list = [];
transform(value: any[], args?: string): any[] {
this.list = [];
if ( typeof value !== 'undefined' ) {
value.forEach(val => {
if (val.group.indexOf(args) > -1) {
this.list.push(val);
}
});
return this.list;
}
}
}