计算平均数出现错误
x = 0count = 0
a = int(input('请输入数子:'))
while a.isdigit():
x = x + a
count +=1
a = int(input('请第%s次输入数子:'%(count-1)))
print("本次计算的平均数为:",x/count)
想利用输入非数字结束计算,但是一直报错,请大神告知正确方法,谢谢 .isdigit是str类方法,你要么先判断完再转成int x = 0
count = 1
a = input('请输入数子:')
while a.isdigit():
x = x + int(a)
count += 1
a = input('请第%s次输入数子:' % count)
print("本次计算的平均数为:", x / count) 二楼说的对 额你确定这个能计算出平均数?{:10_256:}
isdigit() 方法检测字符串是否只由数字组成。
{:10_257:} 楼上的算法有点错误,count初始值为1的时候,最后应该是除以(count-1),为0的时候才是count:
x = 0
count = 0
a = input('请输入数字:')
while a.isdigit():
x += int(a)
count += 1
a = input('请第%s次输入数字:' % (count+1))
print("本次计算的平均数为:",x/count) zltzlt 发表于 2020-4-3 19:26
不好意思,最佳答案点错了,没法撤销。本来是点你的答案的。 heidern0612 发表于 2020-4-3 19:27
二楼说的对
四楼说得对{:10_279:},禁止套娃
页:
[1]