Python Forum

Full Version: Perfect Square program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, all!

I'm new to the forums, and have a small problem on this short program homework assignment. Hopefully somebody can help me out. I'm working on a program to have the user enter a number to see if it's a perfect square. Now I wrote the program in Java (which is my first language) and translated it to Python (which i'm still frankly new with), and the program works 100% how it's supposed to, so here's the code for that:

import math

# get user input
user_number = input("Enter a number to see if it's a perfect square: ")

# define function and calculate
def perfect_square(user_number):
    root = math.sqrt(int(user_number))
    return int(root) ** 2 == int(user_number)

# loop through conditions and output suites
while not perfect_square(user_number):
    user_number = input("The number " + str(user_number) + " is 'not' a perfect square. Try again: ")
else:
    print("\nThe number " + str(user_number) + " is a perfect square! Good job!")
The problem is, I used a method in Java and a Function in Python for my calculation, but since it's so early in the class, we haven't reached the chapter in our textbook where we discuss functions. That being said, my teacher wants me to write this program without using a function, and I'm a little confused on how to do that... Any tips would be great. I know it's super easy, I just can't come up with the solution for some reason.

Thanks!
1) move the code in the function to the first part of the while loop.
2) change return to var =
3) change the while loop header to while not var:
4) add var = False before the while loop start to give it a default value
(Sep-01-2017, 01:59 AM)forumer444 Wrote: [ -> ]Hi, all!

I'm new to the forums, and have a small problem on this short program homework assignment. Hopefully somebody can help me out. I'm working on a program to have the user enter a number to see if it's a perfect square. Now I wrote the program in Java (which is my first language) and translated it to Python (which i'm still frankly new with), and the program works 100% how it's supposed to, so here's the code for that:

import math

# get user input
user_number = input("Enter a number to see if it's a perfect square: ")

# define function and calculate
def perfect_square(user_number):
    root = math.sqrt(int(user_number))
    return int(root) ** 2 == int(user_number)

# loop through conditions and output suites
while not perfect_square(user_number):
    user_number = input("The number " + str(user_number) + " is 'not' a perfect square. Try again: ")
else:
    print("\nThe number " + str(user_number) + " is a perfect square! Good job!")
The problem is, I used a method in Java and a Function in Python for my calculation, but since it's so early in the class, we haven't reached the chapter in our textbook where we discuss functions. That being said, my teacher wants me to write this program without using a function, and I'm a little confused on how to do that... Any tips would be great. I know it's super easy, I just can't come up with the solution for some reason.

Thanks!

Thank you, I appreciate the help, but it's still not running properly. There's still a logic error. Here's the updated code:

import math

# get user input
user_number = input("Enter a number to see if it's a perfect square: ")

#
var = False

# loop through conditions and output suites
while not var:
    root = math.sqrt(int(user_number))
    var = int(root) ** 2 == int(user_number)
    user_number = input("The number " + str(user_number) + " is 'not' a perfect square. Try again: ")
else:
    print("\nThe number " + str(user_number) + " is a perfect square! Good job!")
Thanks, again.
import math
user_number = input("Enter a number to see if it's a perfect square: ")
var = False
while not var:
root = math.sqrt(int(user_number))
var = int(root) ** 2 == int(user_number)
if(var):
print("The number " + str(user_number) + " is a perfect square! Good job!")
else:
print("The number " + str(user_number) + " is not a perfect square. Try again: ")
user_number = input("Enter another number to see if it's a perfect square: ")

import math
user_number = input("Enter a number to see if it's a perfect square: ")
var = False
while not var:
    root = math.sqrt(int(user_number))
    var = int(root) ** 2 == int(user_number)
    if(var):
        print("The number " + str(user_number) + " is a perfect square! Good job!")
    else:
        print("The number " + str(user_number) + " is not a perfect square. Try again: ")
        user_number = input("Enter another number to see if it's a perfect square: ")
(Sep-01-2017, 06:28 AM)rajeev1729 Wrote: [ -> ]import math
user_number = input("Enter a number to see if it's a perfect square: ")
var = False
while not var:
root = math.sqrt(int(user_number))
var = int(root) ** 2 == int(user_number)
if(var):
print("The number " + str(user_number) + " is a perfect square! Good job!")
else:
print("The number " + str(user_number) + " is not a perfect square. Try again: ")
user_number = input("Enter another number to see if it's a perfect square: ")

import math
user_number = input("Enter a number to see if it's a perfect square: ")
var = False
while not var:
    root = math.sqrt(int(user_number))
    var = int(root) ** 2 == int(user_number)
    if(var):
        print("The number " + str(user_number) + " is a perfect square! Good job!")
    else:
        print("The number " + str(user_number) + " is not a perfect square. Try again: ")
        user_number = input("Enter another number to see if it's a perfect square: ")

Oh, that makes sense. Thanks!