Exercise 196 || Solution

Write a program in Python that displays a window to the user that asks them to enter an integer N and displays its factorial N !, as shown in the figure below:

Solution


from tkinter import *

def action():
# getting value of entryNumber
N = int(entryNumber.get())
# initialize factorial
facto = 1
for i in range(1 , N + 1):
facto = facto*i
# display factorial N! in label result
lblResult['text'] = "The factorial of " + str(N) + " is " + str(N) + "! = " + str(facto)


root = Tk()
root.geometry("420x150")

# creating label and entry text for number N
lblNumber = Label(root , text = "Enter value of integer N : ")
lblNumber.place( x = 20 , y = 20)
entryNumber = Entry(root)
entryNumber.place( x = 200 , y = 20 , width = 200)

# creating label to display result
lblResult = Label(root , text ="")
lblResult.place(x = 200 , y = 50 )

# creating button to perform the action
btnValidate = Button(root, text = " Validate" , command = action)
btnValidate.place(x = 200 , y = 90 , width = 200)

root.mainloop()


Younes Derfoufi
my-courses.net

Leave a Reply