Python Forum
Perfect Square program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Perfect Square program
#1
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!
Reply
#2
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
Recommended Tutorials:
Reply
#3
(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.
Reply
#4
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: ")
Reply
#5
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding square roots using long division. jahuja73 10 5,293 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  Magic square! frequency 1 2,505 Dec-17-2018, 06:35 PM
Last Post: micseydel
  Square reverse sum(overloaded) shihomiyano 6 4,023 Aug-18-2018, 06:27 AM
Last Post: micseydel
  Magic Square Puzzle Harnick 1 4,841 Aug-09-2017, 04:51 PM
Last Post: nilamo
  Square Root on calculator MP1234593 1 7,859 Jun-06-2017, 06:58 PM
Last Post: nilamo
  List of square roots python py7 6 6,297 Apr-08-2017, 11:26 PM
Last Post: ichabod801
  Square root of a number mbestivert 1 4,004 Nov-24-2016, 04:35 PM
Last Post: casevh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020