0

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
  • Please format your question/code correctly. – Maurice Meyer Oct 26 '19 at 21:53
  • Just based off the last sentence, you just want all the perfect square numbers smaller or equal to the input? – piokozi Oct 26 '19 at 21:53
  • yes that is what I want. I dont really understand though why my code doesnt show up in the question. I copy and pasted it from python and it looked normal before but now my question just looks like a scrambled mess. – ryley McCafferty Oct 26 '19 at 21:55
  • @ryleyMcCafferty just use the triple backdash ``` around your code for formatting in future. – piokozi Oct 26 '19 at 21:56
  • thanks ya I tried that but it wouldnt let me have it all on one line id have print('''{format for perfect square would go here}''',end=''.format(square)) but it just kept giving me a syntax error – ryley McCafferty Oct 26 '19 at 21:58
  • Keep in mind, that if you use *really large numbers*, this will take a *really long time* in python. It's an easy enough algorithm that you could replicate it in a compiled language, learning only a little bit in your own time. – piokozi Oct 26 '19 at 22:27
  • @ryleyMcCafferty I've updated my solution. Quite a simple one, feel free to use that. – piokozi Oct 27 '19 at 10:09

2 Answers2

0

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.

piokozi
  • 297
  • 4
  • 10
  • Oh sorry I forgot to clarify, he wanted it printed with in the function, and not have anything returned. Thanks though! – ryley McCafferty Oct 26 '19 at 22:10
  • Were also not allowed to use stuff like append(i). He said it would only be what we've learned from the start of the year, so formatted output, inputs, functions, for and while loops. Thanks! – ryley McCafferty Oct 26 '19 at 22:13
  • @ryleyMcCafferty no problem, I can make the changes easily enough :) Just replace the append with a print statement – piokozi Oct 26 '19 at 22:19
  • Thanks! :) I'm trying everyones suggestions, I'm still having a problem with only one value being shown, Ill update my post to show the new code and output. – ryley McCafferty Oct 26 '19 at 22:37
  • @ryleyMcCafferty are you allowed to use list comprehension? – piokozi Oct 27 '19 at 09:20
0

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: '))))
  • Thank you! I'll be taking peoples sugggestions and trying to implement them, I'll update my new code and the final problem I'm still having with the formatted output. – ryley McCafferty Oct 26 '19 at 22:38