Home > Back-end >  "Could not convert string to float:''
"Could not convert string to float:''

Time:05-01

I am a very beginner in terms of coding. I am trying to calculate the sum of the order but python keeps telling me that it could not convert the string to float. Could you please show me where the error lies?

import tkinter as tk

window = tk.Tk()

window.title("Cinema")
window.geometry("700x600")

def total_():
    a= float(e1.get())*2
    b= float(e2.get())*3
    c= float(e3.get())*4
    d= float(e5.get())*1
    e= float(e4.get())*12
    totl = a b c d e
    tax= (totl * 0.13   0.13)
    lblAns['text'] = "Your total is {}$".format(totl)

#Cinema name
Cine_nom = tk.Label(window)
Cine_nom['text'] = "Welcome to cinema !"
Cine_nom['font'] = "Arial 20 bold"
Cine_nom.place(x=350, y=20, anchor="center")

# Menu Creation
label1 = tk.Label(window,
text="Menu",
font="times 28 bold")

label1.place(x=520, y=70)

label2 = tk.Label(window, text="Popcorn (P) \
2$", font="times 18")

label2.place(x=450, y=120)

label3 = tk.Label(window, text="Popcorn (M) \
3$", font="times 18")

label3.place(x=450, y=150)

label4 = tk.Label(window, text="Popcorn (L) \
4$", font="times 18")
label4.place(x=450, y=180)

label5 = tk.Label(window, text="Burger Combo \
12$", font="times 18")

label5.place(x=450, y=210)

label6 = tk.Label(window, text="Soda \
1$", font="times 18")

label6.place(x=450, y=240)

# Entry Table
label9 = tk.Label(window, text="Select the items",
font="times 18")
label9.place(x=115, y=70)

label10 = tk.Label(window,
text="Popcorn (P)",
font="times 18")
label10.place(x=20, y=120)

e1 = tk.Entry(window)
e1.place(x=20, y=150)

label11 = tk.Label(window, text="Popcorn (M)",
font="times 18")
label11.place(x=20, y=200)

e2= tk.Entry(window)
e2.place(x=20, y=230)

label12 = tk.Label(window, text="Popcorn (L)",
font="times 18")
label12.place(x=20, y=280)

e3= tk.Entry(window)
e3.place(x=20, y=310)

label13 = tk.Label(window,
text="Burger Combo",
font="times 18")
label13.place(x=20, y=360)

e4 = tk.Entry(window)
e4.place(x=20, y=390)

label14 = tk.Label(window,
text="Soda",
font="times 18")
label14.place(x=20, y=420)
e5 = tk.Entry(window)
e5.place(x=20, y=450)

# Button for total
btnCalculate = tk.Button(window)
btnCalculate['text'] = "Calculate your bill"
btnCalculate['font'] = "Arial 15"
btnCalculate['command'] = total_
btnCalculate.place(x=30, y=480)

lblAns = tk.Label(window)
lblAns['text'] = ""
lblAns.place(x=40, y=700)

window.mainloop()

CodePudding user response:

It works for me when I add input to the entry boxes. I think you just need to move the statement being displayed. I changed

lblAns.place(x=40, y=700)

to

lblAns.place(x=40, y=550)

I then typed 1, 2, 3, 4, 5 into the five entry boxes (top to bottom). It printed Your total is 73.0$. Given that these are quantities, I think int is a better choice than float.

To set a default value try something like below for each Entry.

e1.insert(0, 0)

Something like below will also work.

def total_():
    a= float(e1.get() or 0)*2
    b= float(e2.get() or 0)*3
    c= float(e3.get() or 0)*4
    d= float(e5.get() or 0)*1
    e= float(e4.get() or 0)*12
    totl = a b c d e
    tax= (totl * 0.13   0.13)
    lblAns['text'] = "Your total is {}$".format(totl)

CodePudding user response:

# use a debugger or print(type(variable)) to check types
def total_():
    a = float(e1.get() * 2)
    b = float(e2.get() * 3)
    c = float(e3.get() * 4)
    d = float(e5.get() * 1)
    e = float(e4.get() * 12)
    totl = float(a   b   c   d   e)
    tax = float((totl * 0.13)   0.13)
    lblAns['text'] = f"Your total is {totl}"
  • Related