Python code question 3

人走茶凉 提交于 2021-01-06 11:54:16

Question 3

Level 1

Question: With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and 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}

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

Solution:

#!/usr/bin/env python
# encoding: utf-8
'''
@author:
@contact:
@software:
@file: q3.py
@time: 2021/1/6 10:57 上午
@desc:
'''

num_input = int(input("Give me a number :"))
result = dict()

for i in range(1, num_input+1):
    result[i] = i*i


print(result)




 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!