Py学习  »  Python

python while循环永不结束(在numworks计算器上)

Laurent • 4 年前 • 1083 次点击  

刚刚获得了一个与python集成的计算器(numworks)。

我正在编写一个python程序,其中包含一个函数来检查输入是否是数字(float)。

当我键入一个正确的浮点数时,一切正常,但当捕捉到异常时,以下是行为:

  • except块运行正常
  • 然后while循环重新开始,再次询问我的输入,输入一个infite循环并冻结。没有时间再输入我的输入。

我不熟悉python,我很确定它是一个简单的语法…但我没能解决。

希望您能帮忙!

代码如下:

    # 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()

干杯

ps:infos关于环境:计算器使用micropython 1.9.4(来源 https://www.numworks.com/resources/manual/python/ )

编辑

这是一个干净的代码版本,里面有你们所有的建议。 把它推到计算器上:就像一个符咒。


    # 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



Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/41250
 
1083 次点击  
文章 [ 2 ]  |  最新文章 4 年前
cglacet
Reply   •   1 楼
cglacet    5 年前

我认为最简单的方法是:

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

也可以使用递归版本:

def input_float():
  try:
    return float(input("Give us a number: "))
  except:
    print("This is not a number.")
    return input_float()
Ralph
Reply   •   2 楼
Ralph    5 年前

你需要比较 type 属于 text float . 要做到这一点,保持你原来的逻辑,一种方法是:

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

或者,您可以使用: while not isinstance(ret, float) ( isinstance 实际上是检查python中类型的首选方法)。

正如@iguananaut在评论中提到的,您可以通过删除 ret 变化无常。

def input_float():
    while True:
        try:
            return float(input("(nombre): "))
        except ValueError:
            print("saisie incorrecte.")

编辑

让这个和你的 test 功能和你的第二个 while 循环需要在以下情况下包含break子句 test() 返回:

# 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

注意,如果您选择使用我建议的第二个函数( input_float )您需要将测试函数更改为

# test
def test():
  print(input_float())