【发布时间】:2018-03-04 09:15:54
【问题描述】:
所以我正在为我的班级编写这段代码,但我遇到了一个错误,我不确定它为什么会返回错误。 这是返回此错误的代码部分:
numValues = int(input("Input number of inputs (between 1 and 15): "))
newList = []
def input_nums():
inputs = float(input("Enter a float value: "))
newList.append(inputs)
return newList
while numValues:
input_nums()
numValues -= 1
def in_order():
for i in newList:
print(i)
def rev_order():
for j in newList[::-1]:
print(j)
def abo_bel_equ():
avg = sum(newList) / numValues ## This line gives the error ##
print("The average is: ", avg)
for x in range(numValues):
above_avg = below_avg = equal_avg = 0
if newList[x] > avg:
above_avg += 1
if newList[x] < avg:
below_avg += 1
if newList[x] == avg:
equal_avg += 1
print("The values of the list in order are: ")
in_order()
print("The values of the list in reverse order are: ")
rev_order()
print(abo_bel_equ())
“numValues”变量应该是用户输入的任何整数,而是返回 0。
为什么这个函数说 numValues 变量应该是用户输入的整数时为零?
【问题讨论】:
-
为什么不尝试添加
if语句来检查numValues的值 -
从这段代码本身来看,除非用户输入“0”,否则应该没有问题。我们能看到更多代码吗?
-
我测试了这段代码,没有错误。
-
是的,我只是粘贴我的整个代码,1 秒。 (更新)
-
@LukeH。将
numValues = len(newList)放在abo_bel_equ()的顶部。
标签: python