Write a program that asks the user to enter an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root**pwr is equal to the integer entered by the user. If no such pair of integers exists, it should print a message to that effect.

This is a question from MIT 6.0001 OpenCourseWare. I'm an absolute beginner (kind of started coding for 3 days) and this program costs me like 3 hours to complete. I made many modifications as running the code - finding mistakes - reviewing - then, modifying.

My concern is: is my way of coding is normal. I am not sure if my computational thinking reflected by this 3-hour period is on the right track to learning coding. Please feel free to judge the style, design and effectiveness of my codes. If you can give me some advice on learning, that would be a great help.

I think there are too many if statements here.

x = int(input("Type in the number: "))
print("The root is " + str(x) + ", the power is 1 ") # A pair of numbers 
                                                     #for any number

for pwr in range(1,5): #Examine by each power
  root = 0
  pwr += 1

  while root**pwr < abs(x):
      root += 1
  if  root**pwr == abs(x):
      if x < 0 and pwr % 2 == 1:# test for negative number with odd power
          root = -root
          print("The root is " + str(root) + ", the power is " + str(pwr))
      elif x < 0 and pwr % 2 == 0:# test for negative number with even power
          print("no match for " + "power ", str(pwr) )
      else:# for positive number
          print("The root is " + str(root) + ", the power is " + str(pwr))
      print("no match for " + "power ", str(pwr))
  • 2
    \$\begingroup\$ The indentation has been fixed and it works per the problem statement. I have edited the title to describe what the code does (per the site standard) and it is now reopened. \$\endgroup\$ Mar 18, 2022 at 4:18
  • 1
    \$\begingroup\$ More specifically, this appears to be Guttag Finger Exercise 3.1. \$\endgroup\$
    – Reinderien
    Mar 18, 2022 at 16:04

1 Answer 1


First, the question itself makes no sense and has a trivial answer:

x = int(input("Type in the number: "))
print(f'{x}**1 == {x}')

In other words, for a root equal to x and a power of one, the condition will always be met, so there is no calculation or looping required. Either the question is open to you realising this and providing the trivial answer, or the question was written lazily and is not a good exercise.

You seem to have already captured this in your "The root is " + str(x) + ", the power is 1 "; but then - why do anything else?

Let's be generous and assume that the question is actually looking for the highest integral power corresponding to an integral root. A loop of the form you've written is still too complex, and can be as simple as

x = int(input("Type in the number: "))

for pwr in range(5, 0, -1):
    root_exact = x ** (1/pwr)
    root_int = round(root_exact)
    if root_int**pwr == x:
        print(f'{root_int}**{pwr} == {x}')

You should not care about odd or negative numbers.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.