0

when I enter small number in the screen then it works fine but when I enter large number it stops working and not responding to any user input I have used OverflowError but it doesn't seems to work. Pls help me out

from tkinter import *
from tkinter import messagebox as msb
import math

root = Tk()
root.geometry("500x200")
# screen to write the number
screen_val = StringVar()
screen = Entry(root, textvariable=screen_val, font="Aerial 33")
screen.pack(fill=BOTH)

factorial_question = ""


def factorial():
    global factorial_question
    try:
        factorial_question = screen_val.get()
        if "Answer : " in screen_val.get():
            factorial_question = factorial_question.replace("Answer : ", "")

        factorial_answer = math.factorial(int(factorial_question))
        screen_val.set("Answer : " + str(factorial_answer))
        screen.update()
    except OverflowError:
        if "Answer : " in screen_val.get():
            factorial_question.replace("Answer : ", "")
        msb.showwarning("Error", "Value too long")

    except ValueError:
        msb.showwarning("Error", "write the correct number")


button_factorial = Button(root, text="!", font="aerial 15 bold", borderwidth=10, bg="light yellow", pady=14,padx=15,command=factorial)

button_factorial.pack()

root.mainloop()

1 Answers1

0

What you need to do is decide on a maximum number allowed, and then you could edit the function so that it utilizes ifs to check if the number is too big or too small. The reason why your try-except is not catching the error is because there is no error, but thee time the computer spends calculating factorials and displaying huge numbers onto the screen grows exponentially and fast.

Basically, use ifs to make your program limit the amount the user can enter, perhaps even adding an else to notify them that the number is too big.

Shufi123
  • 233
  • 3
  • 11