HomePagev2 › Forums › Python Discussions › Loops practice4

Hello @satishvadlamani sir
could you please help with the below problem? I think the answer is correct but it’s showing incorrect.
Define a function which can generate and return a dictionary where the keys are consecutive numbers between x1 and x2 (both included) and the values are square of the keys.
def square_maker(x1,x2):
d = {}
for key in range(x1, x2+1):
d[key] = key**2
return d

Looks correct to me. @treverehrlich can you please check this one as well. Meanwhile, can you think of another approach to the problem? Also, is this code bug proof?
@badrirajasekharreddy and @varadaamrutharatna you folks might have some input here?

def square_maker(x1,x2):
a={i:i*i for i in range(x1,x2+1)}
return a
print(square_maker(9,12))

@badrirajasekharreddy this is a good. Can we do this without a for loop?


Thanks everyone for your input; you are pioneers – the first to try these exercises. Your code should work now.
Log in to reply.