0

Begginer here! I'm starting to learn how to code, and I've encountered an issue...

I created a basic "Maximum number" function, and then took user inputs as the three parameters in the function, but for some reason it's not returning the desired value.

def max_num(num1, num2, num3):
    if num1 >= num2 and num1 >= num3:
        return num1
    elif num2 >= num1 and num2 >= num3:
        return num2
    else:
        return num3


user1 = input("Enter a number --> ")
user2 = input("Enter another number --> ")
user3 = input("Enter one last number --> ")

print("The larger of those numbers is: " + max_num(user1, user2, user3))

After typing the values [user1 = 50], [user2 = 150] and [user3 = 100] as the user, and expecting the function to return [150] as the highest value, this is what I get:

Enter a number --> 50
Enter another number --> 150
Enter one last number --> 100
The larger of those numbers is: 50

Process finished with exit code 0

I tried changing the variables' names in case there was some kind of namespace overlapping, without success.

Also noticed that this happens when the values entered by the user have different sizes (one value has 2 digits while the other two have three digits).

Any ideas on how to solve this? Thank you!!

dippas
  • 58,591
  • 15
  • 114
  • 126
Santi
  • 5
  • 3

1 Answers1

0

The input() function in Python 3 inputs strings (text); those are then compared alphabetically, and indeed 50 is the maximum (last) in alphabetical order.

You'll need to convert them to numbers at some point, using int() or float() or Decimal().

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
  • 1
    Solved by converting user inputs into integers and calling the function as a string... thanks! – Santi Apr 06 '21 at 11:28