-3

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
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask page](https://stackoverflow.com/help/how-to-ask) for help clarifying this question. – Thornily Dec 15 '22 at 18:06
  • 3
    I see you have posted some code. Have you run this code? Does it produce the output you want? Are there any errors when it runs? – quamrana Dec 15 '22 at 18:06
  • 1
    Your code has many problems. 1. `int()` doesn't take a comma-separated input. See https://stackoverflow.com/q/3477502/843953 2. Integers don't have a length. If you do 1 correctly, `n` will be a _list_. There is no `list.len` method. See https://www.google.com/search?q=python+get+length+of+list 3. There is no `list.split` method either. Anyway, you should have already done the splitting in 1. 4. Don't shadow python builtins such as `sum` by creatign variables of that name 5. You add `i` to `sum`, where `i` is the _index_. You want to add the _value_ in the list. – Pranav Hosangadi Dec 15 '22 at 18:14
  • 1
    6. You divide the total by `n`, which is the list containing all numbers, You want to divide it by the length of the list. Since you're asking us how to fix so many problems, your question lacks focus. Please narrow down your question so it asks about one problem only (in which case you should find many duplicates on the internet in general and Stack Overflow in particular) – Pranav Hosangadi Dec 15 '22 at 18:14

2 Answers2

1

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.

J_H
  • 17,926
  • 4
  • 24
  • 44
-1

There are several ways to write your program in python. Referring to your seed code, it should be like this.

[here is the code

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

print(b)

]1

  • The value of x is incorrect. – Mitchell Olislagers Dec 15 '22 at 19:18
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – lemon Dec 18 '22 at 21:05