0

I wrote a code in Python to print the minimum and maximum value of user input. If I input the number 0 to 15, I would expect max() to return 15 and min() to return 0. However, max() returns 9:

i = (input().split(" "))
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

print(min(i))
# 0
print(max(i))
# 9

Why doesn't max(i) return 15?

KenHBS
  • 6,756
  • 6
  • 37
  • 52

2 Answers2

1

If you check out your list i, you will see that the digits you entered are stored as strings, whereas you want to evaluate them based on their numeric value. Python is able to calculate the maximum value of strings, too (see this post for more information).

You can fix this by explicitly transforming your input to integers:

i = [int(x) for x in input().split(" ")]

print(max(i))
print(min(i))
KenHBS
  • 6,756
  • 6
  • 37
  • 52
0

You can also do it like this.

i = list(map(int, input().split(" ")))

print(min(i))
print(max(i))
Cyber
  • 164
  • 7