ValueError:无法将字符串转换为浮点型:pythonGUI

时间:2020-05-17 16:04:29

标签: python python-3.x python-3.7 google-api-python-client

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)

我将使用Python GUI Restront计费管理系统。但是在计算中我遇到了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: 

2 个答案:

答案 0 :(得分:2)

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)

答案 1 :(得分:1)

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

进行检查

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

x = Tea.get()
if x.isnumeric():
    float(x)
else:
    print(f"x is not a float is is: {x}")