ValueError: could not convert string to float: pythonGUI

ValueError: could not convert string to float: pythonGUI

costOfItem = (Item1 * 10) + (Item2 * 20)\
                              + (Item3 * 30) + (Item4 * 40) + (Item5 * 50) + (Item6 * 60) + (Item7* 70) + (Item8* 80)
                 SubTotalofITEMS = "Rs.", str('%.2f'% costOfItem)

                 SubTotal.set(SubTotalofITEMS)
                 Tax="Rs.", str('%.2f'% ((costOfItem) * 0.08))
                 GSTTax.set(Tax)
                 TTax = ((costOfItem) * 0.08)

                 TCost = "Rs.", ('%.2f'% (costOfItem + TTax))
                 TotalCost.set(TCost)

我会 Makaing Python GUI Restront 计费管理 system.But 我在计算 costofItem 时遇到问题.....

 **strong text**
   Error showing 
    t__.py", line 1705, in __call__
        return self.func(*args)
      File "C:\Users\Lenovo\Desktop\Py.Billing system - Copy.py", line 252, in costOfItem
        Item1=float(Tea.get())
    ValueError: could not convert string to float: 

您尝试从字符串转换为浮点数的值可能不是数字,您可以使用内置方法检查这一点 isnumeric

至于该值当前包含什么我不知道,但我建议在它无法查看它是否是您不期望的值之前打印该值。

x = Tea.get()
if x.isnumeric():
    float(x)
else:
    print(f"x is not a float is is: {x}")
costOfItem = (Item1 * 10) + (Item2 * 20)\
                              + (Item3 * 30) + (Item4 * 40) + (Item5 * 50) + (Item6 * 60) + (Item7* 70) + (Item8* 80)
                 SubTotalofITEMS = "Rs.", str('%.2f'% costOfItem)

                 SubTotal.set(SubTotalofITEMS)
                 Tax="Rs.", str('%.2f'% ((costOfItem) * 0.08))
                 GSTTax.set(Tax)
                 TTax = ((costOfItem) * 0.08)

                 TCost = "Rs.", str('%.2f'% (costOfItem + TTax))
                 TotalCost.set(TCost)