 Making square within limit

Hey the task goes like this:

Make a program that will print biggest square under 500 out of number that the user will choose [so int(input()) ] in interval <2;500).
How can I make it while using ‘while cycle’?

Why would you need a while loop when you draw a single square?

You’re right- How do I do it then?

Hi @Anya,

If you could provide us with the code that you have thus far, we can help. Please format any Python code that you post. See About the Users category for advice regarding how to do this.

Thank you. I tried to do it with while cycle and find a way that will print just the largest power within given interval, but I couldn’t find any , so now it prints all the squares until the largest one including the largest one.

n = int(input("enter a number from 2 to 500:"))

power=1
if 2<=n<500:
while power<500:
print(power,end=" ")
power=power*n

else:
print("you entered an incorrect number'")

Thanks for any response

Hi, @Anya ,

In your first post, you referred to squares, but now you seem to be working with higher powers. Also, is this an assignment that specifically requires you to use a while loop?

EDIT (October 23, 2021):

If you, in fact, are expected to be working with powers rather than squares, and if you need to include a while loop, everything that you have written looks good, except for the while loop in its current form.

The condition that controls the loop can be n ** power < 500. Remember to increment power by 1. Then when the loop exits, power will have exceeded its required value by 1. Accordingly, after the loop, subtract 1 from power to get what is required, and display the result as you see fit. One possibility would be as follows:

print("{} ** {} = {}".format(n, power, n ** power))

Following is an example of a possible result:

7 ** 3 = 343

Thank you very much. The assigment didn’t ask for using a while loop, I just didn’t really know where to go with it, so I used the while loop. Anyways thank you very much for your answer, I really appreciate it.

Using a while loop is fine. There’s more than one way to do this. A for loop would be another viable approach.

Have you come up with a satisfactory solution? If not, we’ll be happy to help, if you post your most recent code along with questions.