我的程序应该从用户那里获取输入并提供平均值。如果输入的数字小于0,则应该结束。我的程序陷入了一个循环,要求用户输入一个数字而不是计算平均值。
a=-1
s=0
c=0
I=input('Please enter a number')
I=float(I)
while I >= 0:
s += I
c +=1
I=input('Please enter a number')
I=float(I)
if c>0:
a= s/c
print(a)
else:
print("No input, cannon compute the average")
答案 0 :(得分:0)
这样做
a=-1
s=0
c=0
I=input('Please enter a number')
I=float(I)
while I >= 0:
s += I
c +=1
I=input('Please enter a number')
I=float(I)
if c>0:
a= s/c
print(a)
else:
print("No input, cannon compute the average")
答案 1 :(得分:0)
theSum = 0
counter = 0
numberEntered = float(input('Please enter a number: '))
while(numberEntered >= 0):
theSum += numberEntered
counter += 1
numberEntered = float(input('Please enter a number: '))
if counter > 0:
print(theSum/counter)
else:
print("No input, cannot compute the average.")