Python 3.x 带循环和决策树的小解算器程序,为什么不工作?

Python 3.x 带循环和决策树的小解算器程序,为什么不工作?,python-3.x,loops,decision-tree,solver,Python 3.x,Loops,Decision Tree,Solver,如果您能帮助我解决此代码中的错误,我将非常感激 while循环不起作用 当程序要求计算时,它停止 总体思路是让这个小程序在计算过程中帮助我,如果可能的话,还可以添加一个可视化界面 def cycle(): while True: newcalc = input('Do you want to make a new calculation? \n \t (Y/N)') if newcalc.lower() in ["no","n"]: newcalc = "no"

如果您能帮助我解决此代码中的错误,我将非常感激

  • while循环不起作用
  • 当程序要求计算时,它停止
  • 总体思路是让这个小程序在计算过程中帮助我,如果可能的话,还可以添加一个可视化界面

    def cycle():
    while True:
        newcalc = input('Do you want to make a new calculation? \n \t (Y/N)')
        if newcalc.lower() in ["no","n"]:
            newcalc = "no"
            break
        if newcalc.lower() in ["yes","y"]:
            newcalc = "yes"
        else:
            print('This is not the right answer')
    
    def counting_M():
      while True:
        concersion_1 = input('Do you want to convert from M to uM? (Y/N)')
        if concersion_1.lower() in ["y", "yes"]:
           print('This is the result of the conversion {} uM',   str(data/1000)).format(data)
        mw = input('What is the Molecular Weight of your compound, please in g/M?')
        print('Your coumpound weights ug/ml', str((data) / (mw / 1000))).format(data)
        if mw in float:
            print('The data you have entered is not numeric')
            break
    data = input('Please, provide the absolute amount of compound you have, without units \n \t')
    units = input('Please, indicate the measure unit you have \n  A)M, B)mM, C)uM '
              '\n 1A)g/liter, 2A)mg/ml, 3A)ug/ml \n \t')
    if units.lower() == "a" :
     print('Ok so you have M')
     counting_M()
     cycle()   
    

    谢谢你

    你做错了几件事:

    • def cycle()
      不返回
      newcalc
      ,呈现其赋值 无用,因为它在函数之外的任何地方都不可见
      循环
    • 似乎您正在分配
      数据
      两次,同时 未从
      字符串
      转换为
      浮点
      ,这会使
      打印崩溃
      命令,无论何时返回算术运算(即。,
      str((数据)/(mw/1000))
    • 如果浮动中的mw不存在,则行
      
      检查
      mw
      是否为
      float
      。使用
      try/except
    例如:

    try:
        float('0.001')
    except ValueError:  # occurs when the string cannot be converted to a  valid float)
        print('Not a valid float string!')
    
    虽然我不清楚您到底想要实现什么,但这段代码应该完成您在示例中尝试的所有事情

    def cycle():
        choice = None
        while not choice:
            newcalc = input('Do you want to make a new calculation? \n \t (Y/N)')
            if newcalc.lower() in ["no", "n"]:
                choice = "no"
    
            if newcalc.lower() in ["yes", "y"]:
                choice = "yes"
            else:
                print('This is not the right answer')
                continue
        return choice
    
    def counting_M(data):
        while True:
            concersion_1 = input('Do you want to convert from M to uM? (Y/N)')
            if concersion_1.lower() in ["y", "yes"]:
                print('This is the result of the conversion {} uM'.format(str(data/1000)))
            else:
                print('Not converting')
            while True:
                mw = input('What is the Molecular Weight of your compound, please in g/M?')
                try:
                    mw = float(mw)
                    break
                except ValueError:
                    print("Not a valid float!")
    
            print('Your coumpound weights ug/ml', str((data) / (mw / 1000)))
    
    if __name__ =='__main__':
        while True:
            data = input('Please, provide the absolute amount of compound you have, without units \n \t')
            try:
                data = float(data)
                break
            except ValueError:
                print("Not a valid float value!")
    
        counting_M(data)
        choice = cycle()
        print(choice)
    

    什么是
    单位
    ?什么是
    mw\u计算
    ?请把缩进的部分修好。这至少在两个方面是错误的,目的是在我必须测试化合物时,为我做一些非常方便的计算。非常感谢你。你们的代码很清楚,我不熟悉,除了继续一个。