0

I want to write function, in-order to convert string, with many parameters, into integer. Problem is, how to define?

Like

def function_average(numbers)

where numbers is one string.

I want

function_average('40,50,60')
>>> 50

without cutting into different strings, but in one.

I tried different methods, but always have a mistakes, like invalid syntax.

3
  • 2
    Please share the code you have tried, and the errors you got. Commented Feb 21, 2016 at 8:14
  • 2
    Also show us the expected result. It's not clear what you want. Commented Feb 21, 2016 at 8:15
  • Oh, sorry, I will rewrite! Commented Feb 21, 2016 at 8:24

1 Answer 1

1

just guess, from your function name and parameters ...

def function_average(numbers):
    numbers_list = numbers.split(',')
    return sum([int(x) for x in numbers_list]) / len(numbers_list)


print(function_average('40,50,60'))
Sign up to request clarification or add additional context in comments.

4 Comments

Oh, this is interesting. So, split will separate strings?
sum(int(x) for x in numbers_list) no need to make list, when you can use generator
But what about ('3.4,3.5,6.7')? Is it other split for such numbers? Oh, I got, need to use float, instead of integer. Thanks!
@LukaRahne, yes, for sum no need to make list, but also need get the length of the number list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.