Solution Exercise 11

With a given number n, write a program to generate a dictionary that contains (i, i*i). The dictionary shall include all integer values for i from 1 to n (both included). Then the program should print the dictionary.
Suppose the following input is supplied to the program:
8
Then, the output should be:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}          

Hint:
In case of input data being supplied to the question, it should be assumed to be a console input. Consider use dict()

def main():
    n = input("Enter a number: ")
    #Create empty dict
    d = {}
    #As the result from input is always a string we need to 
    #convert to an int with the help of int(n)
    #We will also need to add 1 to n so that n is included in the range
    for i in range(1,int(n)+1): 
        d[i] = i*i #We could also write i**2 instead of i*i
    print(d)
           
if __name__ == '__main__':
    main()