1

First post here;

I'm trying to find if an inputted number is a perfect square. This is what i've come up with (im a complete first timer noob)

import math

num = int(input("enter the number:"))

square_root = math.sqrt(num)
perfect_square = list[1, 4, 5, 6, 9, 00]
ldigit = num%10

if ldigit in perfect_square:
     print(num, "Is perfect square")

The list are digits that if the integer ends in, it will be a perfect square.

perfect_square = list[1, 4, 5, 6, 9, 00]

TypeError: 'type' object is not subscriptable

Never seen this before (surprise). Apologies if it's a total mess of logic and understanding.

3 Answers3

3

You have an error in your code:

perfect_square = list[1, 4, 5, 6, 9, 00]

Should be:

perfect_square = ['1', '4', '5', '6', '9', '00']

Secondly these are defined as ints, so you cannot have a number 00, instead convert everything to string to do the check and then back to ints with str and int.

Personally I'd rather go with another approach:

import math

num = int(15)
square_root = math.sqrt(num)

if square_root == int(square_root):
    print(f"{num} is a perfect square")
else:
    print(f"{num} is not a perfect square")
Jurgen Strydom
  • 3,540
  • 1
  • 23
  • 30
0

You declare a list without the keyword 'list', like this:

perfect_square = [1, 4, 5, 6, 9, 00]
ErikXIII
  • 557
  • 2
  • 12
0

We dont need list keyword in order to create List object in python .

List is a python builtin type. List literals are written within square brackets [].

For Example : squares = [1, 4, 9, 16]

squares is a List here .

Ashutosh