
Patrick B. answered 02/09/21
Math and computer tutor/teacher
#########################################
# Here is the new one....
############################
import math
import random
x=random.randint(1,499)
print("The random number is " + str(x))
perfect_square_flag = False
done_flag = False
t=1
while (done_flag==False):
tSqr=t*t
if (tSqr==x):
perfect_square_flag=True
done_flag=True
break
if (tSqr<x):
t=t+1
else:
done_flag=True
if (perfect_square_flag):
print(str(x) + " is a perfect square with square root "+ str(int(math.sqrt(x))))
else:
print(str(x) + " is not a perfect square with square root " + str(round(math.sqrt(x),2)))
##################################################################
#------------------------- Here is the OLD one
############################################################
import math
import random
perfect_squares=[1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484]
x=random.randint(1,499)
perfect_square_flag=False
for n in perfect_squares:
if int(n)==int(x):
perfect_square_flag=True
break
if (perfect_square_flag):
print(str(x) + " is a perfect square with square root "+ str(int(math.sqrt(x))))
else:
print(str(x) + " is not a perfect square with square root " + str(round(math.sqrt(x),2)))
Ethan H.
is there a simpler way to do it02/09/21