代码之家  ›  专栏  ›  技术社区  ›  Danny

无法将字符串转换为浮点数:“”

  •  0
  • Danny  · 技术社区  · 3 年前

    就编码而言,我是一个非常初学者。我试图计算顺序之和,但python一直告诉我它无法将字符串转换为float。你能告诉我错误在哪里吗?

    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()
    
    2 回复  |  直到 3 年前
        1
  •  0
  •   Steve    3 年前

    当我向输入框中添加输入时,它对我有效。我想你只需要移动正在显示的语句。我变了

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

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

    然后我在五个输入框(从上到下)中输入1、2、3、4、5。上面印着 Your total is 73.0$ .考虑到这些都是数量,我认为 int 是一个比 float .

    要设置默认值,请为每个条目尝试以下操作。

    e1.insert(0, 0)
    

    下面这样的方法也可以。

    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)
    
        2
  •  0
  •   Subramanya Rao    3 年前
    # 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}"