0

So I am new to python. Have written code in java years ago. As part of a beginner friendly udemy course I wrote the following program:

print("Please Enter the first number: ")
num1 = input()
print("Please Enter the second number: ")
num2 = input()

def greaterOfTwo(num1, num2):
    if num1 > num2:
        return num1
    else:
        return num2

print("The greater number is:", greaterOfTwo(num1, num2))

The weird part is my program only works for certain cases. for e.g. if I enter num1 as 5 and num2 as 10 the program returns 5 as the greater number:

Please Enter the first number:
5
Please Enter the second number:
10
The greater number is: 5
  • You have to convert the user inputs to numbers, because python is interpreting them as strings. I suggest either changing the arguments in the function to floats like this: float(num1), or changing the variables to floats during the user input, like this: num1 = float(input()) – Chowlett2 Aug 04 '22 at 03:19
  • In case there is any confusion, you are comparing strings, where "2" is greater than "19999999" because of that first character. One trick is to print the "repr" of a python value which gives you better type hints than the default `str` value print uses: `print("The greater number is:", repr(greaterOfTwo(num1, num2)))`. You'll see a quoted string. – tdelaney Aug 04 '22 at 03:20

0 Answers0