-1

Now i am calculating the total for the following entries . if i dont give an input i want it to consider the input as zero , however if there is no input there is no total computation . a simple variable=='' detection.

#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)

now if i enter all of it then i get the total computed correctly , if i dont enter values it dosent compute total.Please suggest!

user9093127
  • 181
  • 2
  • 3
  • 12

1 Answers1

0

You can do

if Fries.get(): 
    cof = float(Fries.get()) 
else: 
    cof = 0

or using special construction with if/else

cof = float(Fries.get()) if Fries.get() else 0

But someone can put text or incorrect float so better use try/except to catch it

try:
    coF = float(Fries.get())
except ValueError:
    coF = 0
furas
  • 134,197
  • 12
  • 106
  • 148