StudentOIST
StudentOIST

Reputation: 189

python error message min() arf is an empty sequence

I am trying to create a function that takes up a string of numbers and outputs the maximum and minimum values. Here is my code

def high_and_low(numbers):
    numbers = map(int, numbers.split())
    max_n = max(numbers)
    print(max_n)    
    min_n = min(numbers)
    return(max_n, min_n)

But I get the following error: ValueError: min() arg is an empty sequence. So I assume that it does not read the negative values, but I dont know why..

Upvotes: 0

Views: 165

Answers (1)

jasonharper
jasonharper

Reputation: 9597

I assume you're using Python 3.x, where map() was changed to return a generator rather than an explicit list of results as in Python 2.x. Calling max() on this generator exhausted it, leaving no elements for min() to iterate over.

One solution would be to convert this generator to a list, perhaps numbers = list(numbers) as the second line of your function. As a list, you can iterate it as many times as you need to.

Upvotes: 1

Related Questions