-1

I have a question about the task below. I'm trying to find a way to convert the list I write into integers but seems I'm unable to find a way to do so. This is the first question.

list1 = input().split(',')  # removing the "," from the string of numbers
average = 0
sunOfNum = 0
for x in list1:  # each element of the list(string in my case) is being iterrated through 
    # and then it should go into sumOfNum but it's a string this is the issue I cannot get pass
    sunOfNum += x
    average = sunOfNum / len(list1)
print(average)

I believe if I can get the list into separated ints the solution of this task will be easy. But how to get it is the question. I read the TypeErr and understand it, tried with setting ints but it doesn't work as it's a list. Any help on this will be appreciated. I've also checked other solutions but they have a pre-defined list not one that you input into the console. Also I've checked the statistics.mean function but that is not how I'd like to solve it, with input I get other errors making things a bit more complicated.

The second question is would this condition actually matter and in which case? "Maintain the relative order of numbers."

Write a program that calculates the average of a list of numbers. Display the average, all the numbers below the average, and all the numbers above the average. Maintain the relative order of numbers.

Input

On the only line of input, you will receive the numbers, separated by a comma. Output

On the first line, print the average, with two digits after the decimal separator. On the second line, print all the numbers bellow the average On the third line, print all the numbers above the average.

Input

3,-12,0,0,13,5,1,0,-2

Output

avg: 0.89
below: -12,0,0,0,-2
above: 3,13,5,1
Samwise
  • 68,105
  • 3
  • 30
  • 44
gginchev
  • 19
  • 3

2 Answers2

0

After you split the list into a bunch of strings, you want to convert those individual strings to ints. An easy way to do this is a list comprehension:

nums = [int(n) for n in input().split(",")]

Now you can easily get the average:

average = sum(nums)/len(nums)  # same as average = statistics.mean(nums)
print(f"avg: {average:.2f}")

and use join to reconstruct the comma-separated list from nums after filtering it:

print(f"below: {','.join(str(n) for n in nums if n < average)}")
print(f"above: {','.join(str(n) for n in nums if n > average)}")

producing:

3,-12,0,0,13,5,1,0,-2
avg: 0.89
below: -12,0,0,0,-2
above: 3,13,5,1
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • Thanks a lot! You guys are just awesome!!!! I was looking for something like this for a few hours and reading all kinds of posts and different blogs about it, but this simple solution made my day! <3 – gginchev Mar 06 '22 at 21:08
0

This is very simple in python you can just cast it to a int. Here's an example.

    list1 = input().split(',')  # removing the "," from the string of numbers
    average = 0
    sunOfNum = 0
    for x in list1:  # each element of the list(string in my case) is being iterrated through 
        # and then it should go into sumOfNum but it's a string this is the issue I cannot get pass
        sunOfNum += int(x)
        average = sunOfNum / len(list1)
    print(average)

For the other issue, I will need to see some code to help with that. You should consider creating a new thread.

thesonyman101
  • 791
  • 7
  • 16