现在我正在计算以下条目的总数。 如果我不给出输入,我希望它将输入视为零,但是如果没有输入,则没有总计算。 一个简单的变量==''检测。
#getting the quantity of fries etc
coF=float(Fries.get()) #cost of fries
coD=float(Drinks.get())
coFilet=float(Filet.get())
coBurger=float(Burger.get())
coChicken=float(Chicken_Burger.get())
coCheese=float(Cheese_Burger.get())
#computation
costofFries=coF * 300 #store whatever is entered in widget
costofDrinks=coD * 300
costofFilet=coFilet * 200
costofBurger=coBurger * 100
costofChicken=coChicken * 150
costofCheese=coCheese * 100
#total calculation
paytax=((costofFries+costofDrinks+costofFilet+costofBurger+costofChicken
+costofCheese)*0.2)
TotalCost=(costofFries+costofDrinks+costofFilet+costofBurger+costofChicken
+costofCheese)
Ser_Charge=((costofFries+costofDrinks+costofFilet+costofBurger+costofChicken
+costofCheese)/99)
现在,如果我输入所有这些,那么我会正确计算总数,如果我不输入值,它就会计算总计。请指教!
你可以做
if Fries.get():
cof = float(Fries.get())
else:
cof = 0
或将特殊结构与 if/else 一起使用
cof = float(Fries.get()) if Fries.get() else 0
但是有人可以放置文本或不正确的浮点数,因此最好使用try/except
来捕获它
try:
coF = float(Fries.get())
except ValueError:
coF = 0