This is from finger exercise 3.1 in Introduction to Computation and Programming Using Python.
Write a program that asks the user to input an integer and prints two integers,
rootandpwr, such that0 < pwr < 6androot**pwris equal to the integer entered by the entered. If no such pair of integers exists, it should print a message to that effect.
I got damn close, thanks in part to some suggestions in Stack Overflow. But nobody got quite all the way there, partially because the point of the exercise is to use nested While loops, and not For loops or anything else more complex than that. It's an exercise is exhaustive enumeration.
I'm really really close. Here's what I have so far:
num = int(raw_input('Enter a positive integer: '))
power = 0
root = 0
while power < 6:
if root ** power == num:
break
power += 1
root= 0
while root ** power < num:
root += 1
if root**power == num:
print('Root = ' + str(root) + ' and power = ' + str(power))
else:
print('No pair of integers exist such that one has an exponent that is between 1 and 6')
There's just two problems here: I have to run a check to see if root and power equal user input and that feels unnecessary. Was wondering how to make that cleaner.
Also when user inputs 1 for input, the program outputs Root 0 and Power 0 which is outside of the parameters of the exercise.
0 < pwr < 6meanspowershould never equal0.