python中exhuastive enumeration的练习疑问

问题遇到的现象和发生背景

要求用户输入一个正整数,运行完代码后可以得到两个正整数root和pwr,使得root**pwr等于该正整数。若没有这样的root和pwr,则应打印消息反映该情况

问题相关代码,请勿粘贴截图

user = int(input("enter a integer =! 1 or 0:"))
noFound = True

for pwr in range(2,6):
for root in range(2, abs(user+1)):
if rootpwr >= abs(user):(粗体rootpwr之间显示不出来的是两个乘号)
break
if root
pwr == abs(user):
noFound == False
break

if noFound == True:
print("There is no perfect root and pwr for your input!")
else:
print("The root is",root,"and the pwr is",pwr)

运行结果及报错内容

无论输入什么数值,最后都显示“There is no perfect root and pwr for your input!”

我的解答思路和尝试过的方法

另外的解答方法如下
root = 0
while root <= abs(user):
for pwr in range(0,6):
if root**pwr == abs(user):
if user >= 0:
print("The root is",root,"and the pwr is",pwr)
elif user < 0 and pwr%2 == 1:
root = -root
print("The root is",root,"and the pwr is",pwr)
else:
print("There is no root and pwr for your input!")
break
else:
root += 1

if root > abs(user):
print("There is no perfect root for your input!")

我想要达到的结果

对于任意输入的正整数(先简化来),都能得到其开方根和相应的指数

代码1中 noFound == False 两个==是比较
要改成 noFound = False 一个=才是赋值

user = int(input("enter a integer =! 1 or 0:"))
noFound = True
for pwr in range(2,6):
    for root in range(2, abs(user+1)):
        if root**pwr >= abs(user):
            break
    if root**pwr == abs(user):
        noFound = False   # == 改成 =
        break

if noFound == True:
    print("There is no perfect root and pwr for your input!")
else:
    print("The root is",root,"and the pwr is",pwr)

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

你这个代码会有一定的局限性,我改了一下,你看看

user = int(input("enter a integer =! 1 or 0:"))
noFound = True

for root in range(2,user//2+1):
    pwr = 2
    while root**pwr<user:
        pwr+=1
    if root**pwr==user:
        noFound = False
        break
    else:
        continue

if noFound == True:
    print("There is no perfect root and pwr for your input!")
else:
    print("The root is",root,"and the pwr is",pwr)