Exercise 108

Write a python program that asks the user to enter an integer n and return a dictionary whose keys are integers 1, 2, 3, ... n and whose values ​​are 1! , 2! , 3! , ... , n!

Solution

# create a function which calculates the factorial of an integer n
def fact(n):
facto = 1
for i in range(1, n + 1):
facto = facto*i
return facto

# create an empty dictionary which will contains the values of the typed integers
d = dict({})
n = int(input("Enter value of integer n"))
for i in range(1, n):
d[i] = fact(i)

# display obtained dictionary d
print("The obtained dictionary is d = " , d)
# The output is :
#The obtained dictionary is d = {1: 1, 2: 2, 3: 6, 4: 24, 5: 120, 6: 720, 7: 5040, 8: 40320, 9: 362880, 10: 3628800}

Younes Derfoufi
my-courses.net

Leave a Reply