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)?
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.
Comments