Beginning Python: For Loop- what do to afterwards
<p>Found this exercise in a textbook online and am attempting to battle my way through it. I believe they intended it to be answered with a while loop, but I'm using a for loop instead which could be my problem.</p>

<p>"Write a program that prompts the user to enter an integer and returns two integers <em>pwr</em> and <em>root</em> such that 0 &lt; pwr &lt; 6 and root**pwr is equal to the integer entered by the user. If no such pair of integers exists, it should print "No such integers exist."</p>

<p>How can I test to see if any #s meet the criteria and if none do, say that?</p>

<pre><code>u_i = int(input("input a #:"))
root = u_i**.5

for pwr in range(1,6):
    ans = root**pwr

    if ans == u_i:
        print(pwr)
        print(int(root))
#here is where the problem comes in as it will do this every time and I'm\ failing to discover what I should do instead.
if ans!= u_i:
    print("No such integer exists")
</code></pre>

<p>Full disclosure: Its been a long time since my last math class so I'm not convinced my "solution" is even correct for what the question is asking. I'm more interested in solving the dilemma I'm facing however as I'm trying to wrap my head around using loops properly.</p>

<p>How about this?</p>

<pre><code>userNum = int(input("Choose a number: "))

for i in range(1,6):
  for j in range(userNum):
    if j ** i == userNum:
      pwr = i
      root = j

if pwr:
  print('pwr = {}, root = {}'.format(pwr, root))
print("No such integers exist.")
</code></pre>

python if-statement for-loop