3

I am trying to solve Finger Exercise 3.1, and I can't figure out what I am doing wrong here. When I enter '1' as the integer, it returns 0 and 0.

I am a complete newbie to programming and Stack Overflow, so I'm not sure if I am doing this correctly, but I figured I would give it a shot.

This is the problem: 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.

And here is my solution thus far:

x = int(raw_input('Enter a positive integer: '))
root = 0
pwr = 0
while pwr < 6:
    pwr += 1
    while root**pwr < x:
        root += 1
if root**pwr == x:
    print "The root is " + str(root) + " and the power is " + str(pwr)
else:
    print "No such pair of integers exists."

How can I fix my code so that it returns the correct integers? What am I doing wrong here? What logic am I missing?

4
  • When I copy and paste your code as written, and enter "1", I get a return of "The root is 1 and the power is 6" I think I would re-write in this special case so that the power does not go all the way to 6, but I am not getting your error. Are you running exactly what you have posted here? But, while we're on the subject, technically speaking, zero to the zero power is indeed equal to one. Commented May 12, 2015 at 23:03
  • This code has a lot of problems. Instead of starting with code, figure out how you would do it by hand. Then translate that algorithm into code. Commented May 12, 2015 at 23:07
  • I don't think you can have stated the problem correctly. Any integer x has x**1=x (ie: root=x, pwr=1), so the program could just do print x, 1. Maybe you meant 1 < pwr < 6? Commented May 13, 2015 at 0:56
  • Thank you guys for your help. I am completely new to programming, so I am sure that my code has a ton of issues. Haha. I hope to learn with time how to program more efficiently. Commented May 13, 2015 at 2:34

2 Answers 2

2

One problem is that, while you do have conditions that end your loops, they will always go all the way up to the maximum allowed condition. You could solve that with break or, as shown, by using return in a function. Also, instead of using a counter, use the xrange() function (range() in Python 3).

>>> def p(num):
...     for power in xrange(6):
...         for root in xrange(num/2+1):
...             if root**power==num:
...                 return root, power
...
>>> r, pwr = p(8)
>>> print 'The root is', r, 'and the power is', pwr
The root is 2 and the power is 3
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I haven't seen the xrange() function before, so I will definitely look at the documentation and search it out.
1

Albeit not very idiomatic for Python, you are close to a correct answer.

The first problem is that you never reset root so you will execute the inner loop only once. Moving root = 0 to the outer loop should fix it.

The second error is that you never break off the loop when you reach the condition you seek. Moving the test to inside the loop will fix this.

Lets see how are we doing so far:

x = int(raw_input('Enter a positive integer: '))
pwr = 0
while pwr < 6:
    root = 0
    pwr += 1
    while root**pwr < x:
        root += 1
        if root**pwr == x:
            print "The root is {} and the power is {}".fornat(
               root, pwr
            )
else:
    print "No such pair of integers exists."

This outputs:

Enter a positive integer: 16
The root is 16 and the power is 1
The root is 4 and the power is 2
The root is 2 and the power is 4
No such pair of integers exists.

Since it is a learning exercise, I will let you figure out and fix other problems with your code.

2 Comments

Thank you. This helped me a lot. I didn't think about putting 'root=0' inside the while loop. I will also work on my overall code. I am a newbie, and I hope to get better with time. Thanks again!
You are going well, Jen, keep up practicing this kind of code kata and you will be a black belt in no time. If you are thinking about being a pro some day, I suggest reading "Clean Coder" from Uncle Bob.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.