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


Messages In This Thread
Perfect Square program - by forumer444 - Sep-01-2017, 01:59 AM
RE: Perfect Square program - by metulburr - Sep-01-2017, 02:14 AM
RE: Perfect Square program - by forumer444 - Sep-01-2017, 04:45 AM
RE: Perfect Square program - by rajeev1729 - Sep-01-2017, 06:28 AM
RE: Perfect Square program - by forumer444 - Sep-01-2017, 09:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding square roots using long division. jahuja73 10 5,460 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  Magic square! frequency 1 2,557 Dec-17-2018, 06:35 PM
Last Post: micseydel
  Square reverse sum(overloaded) shihomiyano 6 4,113 Aug-18-2018, 06:27 AM
Last Post: micseydel
  Magic Square Puzzle Harnick 1 4,887 Aug-09-2017, 04:51 PM
Last Post: nilamo
  Square Root on calculator MP1234593 1 7,970 Jun-06-2017, 06:58 PM
Last Post: nilamo
  List of square roots python py7 6 6,419 Apr-08-2017, 11:26 PM
Last Post: ichabod801
  Square root of a number mbestivert 1 4,070 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