Есть ли способ ввести несколько чисел в функцию, но при вводе 0 скрипт запускается - PullRequest
0 голосов
/ 27 октября 2019

Я пытаюсь создать скрипт, который позволит вам вводить числа, пока не будет введено 0, а затем он вычислит среднее арифметическое или среднее число чисел, которые были введены, кроме 0. Это то, что я имею до сих пор:

a=int(input())
b=0
d=0
while a!=0:
    c=int(input())
    d+=1
    if c !=0:
        break
b=a+c
average=b/c

print(average)

Проблема с этим в том, что он позволяет мне вводить только 2 значения. Заранее спасибо за помощь.

1 Ответ

0 голосов
/ 28 октября 2019

Почему бы вам не попробовать что-нибудь попроще?

a=float(input()) # enter the first input value, it can be also zero
avr=0
t=0 # enter the counter to measure the number of entries
while(a!=0): # you will stop if "a" is a zero
    # if you are here "a" is not zero, so we
    # can add it to the cumulative sum
    avr+=a
    t+=1 # increase the counter
    # re enter the input, if zero the loop will stop without
    # at the next iteration without changing the cumulative sum or the counter
    a=float(input())

# now to avoid division by zero (in case you entered zero as first value), we
# need an if to print out the results
if(t>0):
  print("mean value{}".format(avr/t))
...