vendredi 16 septembre 2016

Beginning Python: For Loop- what do to afterwards

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.

"Write a program that prompts the user to enter an integer and returns two integers pwr and root 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 "No such integers exist."

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

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")

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.

Aucun commentaire:

Enregistrer un commentaire