formatted output with for loops

429 Views Asked by At

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
2

There are 2 best solutions below

0
On

This is a pretty easy exercise: just use for i in range(1, x+1) to get all the numbers smaller than or equal to x that are perfect squares.

Here's what I wrote:

import math

def findSquares(x):
    for i in range(1, x+1):
        if math.sqrt(i).is_integer():
            print("one perfect number is: " + str(i))

It just loops through the relevant numbers.

Another solution is this:

import math

x = int(input("num?: "))
output = "The perfect squares for the number {} are: ".format(x) 
for i in range(1, x+1):
        if math.sqrt(i).is_integer():
                output += "{}, ".format(i)
print(output)

This will make an empty output, and keep adding numbers to that before it outputs.

0
On

Here is a solution that does not use any libraries:

def find_squares(x):
    return_value = []
    for i in range(1, x + 1):
        # Note that x**0.5 is the squareroot of x
        if i**0.5 % 1 == 0:
            return_value.append(i)
    return return_value

print(find_squares(int(input('Please enter a number: '))))