出于某种原因,我的计算结果不正确。我试图做的就是找到用户输入的3个数字的平均值。如果需要更多,请点击这里的def,只要问一下。
#===================== Calculates the average of all three ==========================
def calc_average(self): #average calculation
stop_one_mileage = self.__route[0].stop_one_mileage #stop_one_mileage average
stop_two_mileage = self.__route[0].stop_two_mileage #stop_two_mileage
stop_three_mileage = self.__route[0].stop_three_mileage #stop_three_mileage
avg = int(stop_one_mileage) + int(stop_two_mileage) + int(stop_three_mileage)/3 #adds all three and divides by three
return "<div class='results-container'><span class='title'>Average Mileage: </span><span class='results-container'>" + str(avg) + " miles</span></div>" #returns results
答案 0 :(得分:0)
也许您忘了使用括号?
avg = (int(stop_one_mileage) + int(stop_two_mileage) + int(stop_three_mileage))/3
否则它只将最后一个数字除以3然后将其他数字相加。看看Python operator precedence documentation。
答案 1 :(得分:0)
问题在于:
avg = int(stop_one_mileage) + int(stop_two_mileage) + int(stop_three_mileage)/3
将其更改为:
avg = (int(stop_one_mileage) + int(stop_two_mileage) + int(stop_three_mileage))/3
因为这就是发生的事情:
>>> 2 + 2 + 2 / 3
4
>>> (2 + 2 + 2) / 3
2