ValueError:无法将字符串转换为float:pythonGUI

2024-10-01 05:01:21 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

我将制作PythonGUI限制计费管理系统。但我在计算中面临成本项目的问题

 **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: 

Tags: inpylinefloattaxrssetitem1
2条回答

您试图从字符串转换为浮点值的值可能不是数字,您可以使用内置方法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)

相关问题 更多 >