Reputation: 1
I am creating a function to find the min and max of a dataset, and I get an error when I try to use two return values. I cannot figure out how to format the print function to accept two values that are in the same function.
CODE:
data=[1,5,3,8,9,7,2]
def minMaxList(data):
min=data[0]
max=data[-1]
for i in data:
if i>max:
max=i
if i<min:
min=i
return (min, max)
print("The min and max of this population is {:.2f} and {:.2f}".format(minMaxList(data)))
ERROR:
Type Error: unsupported format string passed to tuple.__format__
Upvotes: 0
Views: 84
Reputation: 346
val = minMaxList(data)
print("The min and max of this population is {} and {}. format (val[0], val[1])
Upvotes: 0
Reputation: 33
You need to use unpacking operator *
So your code should look like
print("The min and max of this population is {:.2f} and {:.2f}".format(*minMaxList(data)))
Upvotes: 1
Reputation: 34
Your code snippet:
data=[1,5,3,8,9,7,2]
def minMaxList(data):
min=data[0]
max=data[-1]
for i in data:
if i>max:
max=i
if i<min:
min=i
return (min, max)
print("The min and max of this population is {:.2f} and {:.2f}".format(minMaxList(data)))
Here you get an issue because the function minMaxList()
returns a tuple/iterative object which needs to be unpacked to resolve issue here, to do this
use * before function call, the print statement should be:
print("The min and max of this population is {:.2f} and {:.2f}".format(*minMaxList(data)))
Upvotes: 0
Reputation: 2086
You can just add an asterisk (*
) before the tuple to unpack it:
print("The min and max of this population is {:.2f} and {:.2f}".format(*minMaxList(data)))
with this addition the program outputs:
The min and max of this population is 1.00 and 2.00
Alternatively,
min, max = minMaxList(data)
print("The min and max of this population is {:.2f} and {:.2f}".format(min, max))
gives the same output and may be more readable.
Upvotes: 1