Python function that gives a maximum of 1.2 [closed]

Tags: ,



I have this code here:

def maximum():
    i = int(float(input("What is the maximum value?")))
    if i < 1.2:
        return print("ERROR: Maximum should be greater than 1.2"), maximum()
    else:
        return i
maximum()

But it doesn’t let me use numbers between 0-1.9 showing an output of

What is the maximum value?1.2
ERROR: Maximum should be greater than 1.2

What should I change in the function to allow decimals to be accepted?

Answer

Remove the conversion to an int, as this will floor your float.

def maximum():
    i = float(input("What is the maximum value?"))
    if i < 1.2:
        return print("ERROR: Maximum should be greater than 1.2"), maximum()
    else:
        return i
maximum()


Source: stackoverflow