Python 如何打印最低根?
我正在写一个程序,用户输入一个数字,这个程序给你这个数字的根和幂。但是,我只希望它打印最低的根(例如:对于Python 如何打印最低根?,python,python-3.x,math,Python,Python 3.x,Math,我正在写一个程序,用户输入一个数字,这个程序给你这个数字的根和幂。但是,我只希望它打印最低的根(例如:对于64,它将打印4**3而不是8**2)。我想知道是否有人可以帮我重新安排循环来实现这一点 n = int(input('enter a number = ')) found = False power = 2 while power < 6 : root = 0 while root ** power < n : root = root + 1
64
,它将打印4**3
而不是8**2
)。我想知道是否有人可以帮我重新安排循环来实现这一点
n = int(input('enter a number = '))
found = False
power = 2
while power < 6 :
root = 0
while root ** power < n :
root = root + 1
if root ** power == n :
print(n, 'is equal to', root, '**', power)
found = True
power = power + 1
if not found:
print(n, 'does not have a root to some power in this range')
n=int(输入('输入一个数字='))
发现=错误
功率=2
功率<6时:
根=0
当根**功率
那么,在找到第一个答案后,是什么阻止您停止循环呢?目前,外部循环在power
上迭代,内部循环在root
上迭代。尝试交换它们,使外循环在根上迭代,内循环在幂上迭代。如何定义“最低根”?在您的示例中,为什么2**6不是“最低根”?@PresidentJamesK.Polk电源不包括6,因此程序不会打印2**6@DYZ第一个答案应该是8**2,因为我的程序首先找到了最低的幂,但我试图只找到第二个答案,它有最低的根,但不一定是最低的幂