data-scientist26
data-scientist26

Reputation: 31

Summing max & min of a list

I'm working on a homework problem that takes a list of comma separated values, converts them to a list, then sums the max & min from said list.

def max_min_sum():
    user_input = input("Please enter a comma seperated list of numbers")
    input_as_a_list = user_input.split(",")
    input_as_numbers = map(float, input_as_a_list)
    print(sum((min(input_as_numbers))+(max(input_as_numbers, default = 0))))
max_min_sum()

Now I'm getting this error:

TypeError: 'float' object is not iterable

I have also tried:

def max_min_sum():
    user_input = input("Please enter a comma seperated list of numbers")
    input_as_a_list = user_input.split(",")
    input_as_numbers = map(float, input_as_a_list) 
    print((min(input_as_numbers) + max(input_as_numbers, default = 0)))    
max_min_sum()

But now it's just giving me the min value.

Upvotes: 2

Views: 51

Answers (1)

Chiheb Nexus
Chiheb Nexus

Reputation: 9257

You've at least 2 errors in your code, see this fix with the comments:

def max_min_sum():
    user_input = input("Please enter a comma seperated list of numbers")
    input_as_a_list = user_input.split(",")
    # map returns a generator, so you need to consume the output first
    # Or, you'll endup with max == 0
    # Or, better using a list comprehension
    # input_as_numbers = [float(elm) for elm in input_as_a_list]
    input_as_numbers = list(map(float, input_as_a_list))
    # No need to use sum, you're already using + operator
    print(min(input_as_numbers)) + max(input_as_numbers, default = 0))

max_min_sum()

Upvotes: 1

Related Questions