4

Just grabbed a calculator with python integrated (numworks).

I'm writing a python program wich includes a function to check if an input is a number (float).

When i type a proper float number everything goes right, but when an exception is catched here is the behavior:

  • the except block is run properly
  • then the while loops restarts, ask my imput again and enter an infite loops and freezes. No time for typing my input again.

I'm not familiar with Python, I'm pretty sure it's a simple syntax thing... But I didn't manage to work it out.

Help would be appreciated!

Here is the code:

    # controle de saisie d'un nombre
    def inputFloat(text):
      ret = ''
      while ret is not float:
        try:
          ret = float(input(text + " (nombre)"))
        except ValueError:
          print("saisie incorrecte.")
      return ret

    # test
    def test():
      print(inputFloat("saisir nombre"))

    # affichage du menu
    while True:
      print("[1] test")
      print("[0] quitter")
      choix = input("Choisir une fonction:")
      if choix == "0":
        print("au revoir.")
        break
      elif choix == "1":
        test()

Cheers

PS: infos about the environnement : the calculator uses MicroPython 1.9.4 (source https://www.numworks.com/resources/manual/python/)

Edit

here is the clean working version of the code with all suggestions from you guys. Pushed it to the calc: works like a charm.


    # controle de saisie d'un nombre
    def inputFloat(text):
      while True:
        try:
          return float(input(text + " (nombre)"))
        except ValueError:
          print("saisie incorrecte.")
          continue

    # test
    def test():
      print(inputFloat("saisir nombre"))

    # affichage du menu
    while True:
      print("[1] test")
      print("[0] quitter")
      choix = input("Choisir une fonction:")
      if choix == "0":
        print("au revoir.")
        break
      elif choix == "1":
        test()
        break



Laurent
  • 61
  • 4
  • `while ret is not float` — `ret` will never be `float`. Perhaps you meant `while not isinstance(ret, float)`. – khelwood Mar 11 '19 at 11:31
  • 1
    First of all the expression `ret is not float` means the value of the variable `ret` is not the `float` type itself. You probably mean `while not isinstance(ret, float)`. But even this is not necessary. You can do without the `ret` variable entirely and just write `while True:` and replace `ret =` with `return`. Then if the entered value successfully converts to a float the value will be returned (and the function, and hence the while loop exited). If an exception occurs then the loop will continue and re-prompt. – Iguananaut Mar 11 '19 at 11:34

1 Answers1

1

I think the simplest way is the following:

def input_float():
  while True:
    try:
      return float(input("Give us a number: "))
    except:
      print("This is not a number.")

You could also use a recursive version:

def input_float():
  try:
    return float(input("Give us a number: "))
  except:
    print("This is not a number.")
    return input_float()
cglacet
  • 8,873
  • 4
  • 45
  • 60