因此,我对python或编码一般还是比较陌生,想知道是否有人可以帮助我完成我作为家庭作业的我的这段代码。所以,我想做的是当我输入一个负数时,我不希望计算负数,但我不知道如何继续。
自动取款机,目前仍在尝试找出是什么循环或什么并不能真正消除负数,但我的笨蛋似乎无法得到它:l
totalSum = 0
n = 0
avg = 0
n = int(input('How many numbers you wish to enter? '))
for i in range(n):
num = eval(input('Enter any number: '))
if num < 0:
break
totalSum += num
avg = totalSum / n
print('Total: ', totalSum, '| ' 'Average: ', avg)
答案 0 :(得分:0)
totalSum = 0
avg = 0
mean = 0
n = int(input('How many numbers you wish to enter? '))
for i in range(n):
num = eval(input('Enter any number: '))
if num < 0:
continue
else:
mean += 1
totalSum += num
avg = totalSum / mean
print('Total: ', totalSum, '| ' 'Average: ', avg)
我认为这就是您要寻找的。您想查找负数并忽略它,但是如果该数字不是负数,则要将其添加到totalSum
输出:
How many numbers you wish to enter? 5
Enter any number: 1
Enter any number: 2
Enter any number: -5
Enter any number: -5
Enter any number: -5
Total: 3 | Average: 1.5