问题遇到的现象和发生背景
要求用户输入一个正整数,运行完代码后可以得到两个正整数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 rootpwr == 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!")
我想要达到的结果
对于任意输入的正整数(先简化来),都能得到其开方根和相应的指数