你好我想知道为什么这段代码不会运行,谢谢。
count = 0
finished = False
total = 0
while not finished:
number = int(input("Enter a number(0 to finish)"))
if number == 0:
finished = True
else:
total = total + number
count = count + 1
print("the average is", total/ count)
count = 0
答案 0 :(得分:0)
它确实运行,我能看到的唯一问题是你的else在if块中缩进。在Python中,缩进很重要,因为卷曲括号或关键字在其他编程语言中很重要。
count = 0
finished = False
total = 0
while not finished:
number = int(input("Enter a number(0 to finish)"))
if number == 0:
finished = True
else:
total = total + number
count = count + 1
print("the average is", total/ count)
count = 0
答案 1 :(得分:0)
你的脚本没问题,你没有正确缩进else
条款,这里有一个工作的例子,你也应该施展你的平均值:
count = 0
finished = False
total = 0
while not finished:
number = int(input("Enter a number(0 to finish)"))
if number == 0:
finished = True
else:
total = total + number
count = count + 1
print("the average is", float(total) / float(count))
count = 0
用几行来做同样的另一种可能的方法是:
values = []
while True:
number = int(input("Enter a number(0 to finish)"))
if number == 0:
break
values.append(number)
print("the average is", float(sum(values)) / float(len(values)))