I want to start off saying I'm an absolute newbie and have been in my program for 2 month only. I'm having trouble displaying the output of my for loop the way I'm required to for a project. I've search all my textbooks and class lectures and can't figure out how to do it. Any help would be appreciated.
I'll copy and paste the function and the output. Keep in mind that this file is just for the function and so doesnt contain the input but I do have that.
Here is what I have, and I'll list below how it needs to come out.
def perfect_square(num):
for number in range(1, num, 1):
square = number ** 2
# print(square, end='')
print("the perfect squares from {} are: {}".format(num, square))
OUTPUT FOR ABOVE
Enter a positive integer: 10
the perfect squares from 10 are: 81
SECOND ATTEMPT
def perfect_square(num):
import math
for number in range(1, num, 1):
square = number ** 2
# print(square, end='')
print("the perfect squares from {} are: {}".format(num, square))
OUTPUT OF ABOVE
Enter a positive integer: 10
the perfect squares from 10 are: 1
the perfect squares from 10 are: 4
the perfect squares from 10 are: 9
the perfect squares from 10 are: 16
the perfect squares from 10 are: 25
the perfect squares from 10 are: 36
the perfect squares from 10 are: 49
the perfect squares from 10 are: 64
the perfect squares from 10 are: 81
The required output needs to look like this
Enter a positive integer: 10
The perfect squares for the number 10 are: 1, 4, 9
Here is the new code and then the output, but above this is the required output I can't seem to figure out. Thanks everyone for the help.
import math
for number in range(1, num):
if number ** .05 % 1 == 0:
print("the perfect squares from {} are: {}".format(num, number))
OUTPUT
Enter a positive integer: 10
the perfect squares from 10 are: 1
This is a pretty easy exercise: just use
for i in range(1, x+1)
to get all the numbers smaller than or equal tox
that are perfect squares.Here's what I wrote:
It just loops through the relevant numbers.
Another solution is this:
This will make an empty output, and keep adding numbers to that before it outputs.