Home > Blockchain >  How to accept multiple numbers and display their average?
How to accept multiple numbers and display their average?

Time:12-16

I tried a lot to write the code but it I was unable to do that the question is that accept input from the user and display average of n given numbers is here any one who can help me with this question here By using Python

n=int(input("Give inputed seprated by comma: "))
x=n.len()
a=n.split(",")
sum=0
for i in range(a):
  sum  =i
b=sum/n

CodePudding user response:

n = int(input("Give inputed seprated by comma: "))

The int() call won't play well with those , commas. Prefer

str_nums = input("Give input numbers separated by comma: ")

Now we have a string (a str), and we wish it was numbers. Let's fix that.

nums = [int(num) for num in str_nums.split(",")]
print("You entered", len(nums), "numbers:", nums)

With that in hand, I believe you'd now be able to compute the mean.

Hint: Feel free to use the sum() function if you'd rather not code up your own loop.

CodePudding user response:

Please get input as string and cast it while sum in loop

Example:

Number = input('input numbers with seperate comma: ')

Number = Number.split()

Sum = 0

For i in Number:

Sum = int(i)

Sum /= len(Number)

Print(Sum)

  • Related