Jump to content

Python - Number Divide Error - Solved

hirushaadi
Go to solution Solved by WereCatf,
2 minutes ago, hirusha.adikari said:

what can i do?

Use float() instead of int(), if you want to convert decimal-numbers.

NOTE: PROBLEM SOLVED! - i converted the int to a float

 

# i think this code is enough to get what i am saying..
from tkinter import *

window = Tk()

e = Entry(window, width=26, borderwidth=5)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

def button_click(number):
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + str(number))
    
def button_addd():
    first_number = e.get()
    global f_num
    global math
    math = "addition"
    f_num = int(first_number)
    e.delete(0, END)
    
def button_equall():
    second_number = e.get()
    e.delete(0, END)
    if math == "addition":
        e.insert(0, f_num + int(second_number))
    elif math == "substraction":
        e.insert(0, f_num - int(second_number))
    elif math == "multiplication":
        e.insert(0, f_num * int(second_number))
    elif math == "division":
        e.insert(0, f_num / int(second_number))
    elif math == "power":
        e.insert(0, f_num ** int(second_number))
        

button_1 = Button(window, text="1", padx=40, pady=20, command=lambda: button_click(1), bg="#FF5733", fg="#FFFFFF")
button_add = Button(window, text="+", padx=39, pady=20, command=button_addd, bg="#C539B1", fg="#FFFFFF")
button_equal = Button(window, text="=", padx=94, pady=20, command=button_equall, bg="#00f636", fg="#000000")

window.mainloop()

 

all of the code work.. but, i can add one to another number, then add one more to the answer.. but when i use division and then try to add something, some conversion error happens ( i think so )

 

the error i get is 

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\hirusha\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "d:/development/Python/Tkinter by codemy/script3.py", line 42, in button_multipyy
    f_num = int(first_number)
ValueError: invalid literal for int() with base 10: '6934088.0'

and i think this is because of the above mentioned reason.. 

 

what can i do?

hey! i know to use a computer

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, hirusha.adikari said:

what can i do?

Use float() instead of int(), if you want to convert decimal-numbers.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×