What is the error in this python program?

Sheikh Ahmad Shah

I am just recently learning to code in python. I am trying to write a program, where given integer will give an output with a root and power (range is set 5 for instance). Here is the code:

user_input = int (raw_input ('Enter your number: '))
root = 1
pwr = 1
def noint ():
    return 'no intergers were found'

def output ():
    return root, '^', pwr
if user_input < 1:
    print noint ()

elif user_input == 1:
    print output ()

else:
    while root < user_input:
        root = root + 1
        if root == user_input:
            print output()   
        else:
            for power in range(5):
                if root ** power == user_input:
                    pwr = power
                    print output()

Now, if I try 25 as an input, output is: (5, '^', 2) (25, '^', 2)

But if I try any prime number like 7, output is:

(7, '^', 1)

What is the problem in coding which is giving me the extra output (25, '^', 2)?

Synergist

If you only want the (root, power) pair with the smallest root value possible, then:

try:
    while root < user_input:
        root = root + 1
        pwr = 1
        for power in range(5):
            if root ** power == user_input:
                pwr = power
                raise Found
except Found:
    print output()

If you want all (root, power) pairs, then:

while root < user_input:
    root = root + 1
    pwr = 1
    for power in range(5):
        if root ** power == user_input:
            pwr = power
            print output()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is error in this fortran program?

What is error in this C program?

What is the error in this object oriented program?

What is causing a parse error in this program?

What's the error in my program?

Python program addition error

What's Wrong In This Python Program

Python error expected an indented block at the end of program file, what is wrong with my code?

Beginner Budget Program Error In Python

Error program built with Flask and Python

What is the proper way to catch an error and halt a program?

What does "error: stray ‘\344’ in program" means?

Unable to run this program. What is the error?

What causes run time error in the following program?

What is this strange error in my c program?

Can anyone explain what is the error in the following program?

What's the best way to terminate a program on error?

what is the error in the below c program-0001?

What are the possible causes of "Abnormal Program Termination" error in a C Program?

Error running Python program: Demystify error message

What are the best ways to have a python program and c program share data

What to put in blanks to complete python program (recursion)

What does this python program do? Was it a RAT?

Is there a way to check what you are running a python program in?

What is going on with scope in this beginners Python program?

Palindrome logic in python: What is wrong with this program?

What's wrong with my python program?

What's wrong with my Python 2 program?

What does this "[0]" mean in a Python program?