-1

Context: I'm learning python and I'm trying to make a function that takes a user input and then tries to convert this input into a float. If this is not possible (for example, if the user types a string), the function should keep prompting the user for valid input. This value should also be between min and max values (0 and 100). I was trying to do this, but I keep getting errors such as "< not supported between instances of 'int' and 'str'" which makes sense since I'm not really converting the user input as an int beforehand so it can't really compare the int value from min and max with the str type of the user input. How could I fix this?

import math

def floatInput(prompt, min=0, max=100):

    is_float = False

    while not is_float:
        try:
            if min < prompt < max:
                res = float(input(prompt))
                return res
            else: 
                if prompt > max or prompt < min:
                    print("The value is not in the interval [0,100]! Try again")
                    res = float(input(prompt))
        except ValueError:
            print("The value can't be converted to float! Try again")
            res = float(input(prompt))
            
            if type(res) == float:
                is_float = True
                return res

    retornar res

def main():
    print("a) Try entering invalid values such as 1/2 or 3,1416.")
    v = floatInput("Value? ")
    print("v:", v)

    print("b) Try entering invalid values such as 15%, 110 or -1.")
    h = floatInput("Humidity (%)? ", 0, 100)
    print("h:", h)

    print("c) Try entering invalid values such as 23C or -274.")
    t = floatInput("Temperature (Celsius)? ", min=-273.15)
    print("t:", t)

    return

if __name__ == "__main__":
    main()
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Tomerikoo Nov 23 '21 at 13:03
  • You are passing the prompt as a string, and you expect it to work with < and > ? - Note: You are assigning value of user input only after reading. before that you are using if condition – Kris Nov 23 '21 at 13:03
  • adding to the other comment, I will discourage to use `min` and `max` as variable name because they represent some python functions – godidier Nov 23 '21 at 14:02

1 Answers1

1

Your every possible input patterns cannot be processed with a standard input process. You have to write different logic for formatting each input format.

In general, you can use the approach in below function to achieve what you want.

def floatInput(prompt, min=0, max=100):
    while True:
        read_in = input(prompt)
        try:
            # parse a standard float representation
            read_in = float(read_in)
        except ValueError:
            try:
                # parse a fractional representation like 1/2 or 3/4
                read_in = eval(read_in)
                if not isinstance(read_in, float):
                    print("The value can't be converted to float! Try again")
                    continue

            except:
                print("The value can't be converted to float! Try again")
                continue

        if not min < read_in < max:
            print("The value is not in the interval [0,100]! Try again")
            continue

        return read_in
Kris
  • 8,680
  • 4
  • 39
  • 67