如何仅打印 for i in range 循环中的一个输入?

How can I print only one of the inputs from a for i in range loop?

x = 64
root = 0

for pwr in range(2,6):
    if x > 1:
        while root <= x:
            root += 1
        
            if root**pwr == x:            
                print('root = ', root, ' and pwr = ', pwr)
        root = 0
    elif x < 0:
        while root >= x:
            root -= 1
        
            if root**pwr == x:            
                print('root = ', root, ' and pwr = ', pwr)
        root = 0
    elif x == 0:
        print('There is no such pair')
        break

我收到的输入是: root = 8 和 pwr = 2 root = 4 和 pwr = 3

但是,我只希望获得最高的根答案(如果有的话)。

所以我希望输入的只是:

root = 8 和 pwr = 2

反之亦然...如果我只想让它显示,我该怎么办: root = 4 和 pwr = 3

谢谢!

您应该创建新变量 max_root, max_pwr,当您计算 root 时,您应该检查它是否大于 max_root 并替换 max_root, max_pwr 或保留旧值。

你应该只在所有计算之后显示

x = 64
root = 0

max_root = 0
max_pwr = 0

for pwr in range(2,6):
    if x > 1:
        while root <= x:
            root += 1
        
            if root**pwr == x:            
                #print('root = ', root, ' and pwr = ', pwr)
                if root > max_root:
                    max_root = root
                    max_pwr = pwr
        root = 0
    elif x < 0:
        while root >= x:
            root -= 1
        
            if root**pwr == x:            
                #print('root = ', root, ' and pwr = ', pwr)
                if root > max_root:
                    max_root = root
                    max_pwr = pwr

        root = 0
    elif x == 0:
        print('There is no such pair')
        break

# after `for`-loop

print('root = ', max_root, ' and pwr = ', max_pwr)

你可以为其他结果做类似的方法,但你可能需要 < 而不是行 root > max_root 中的 > 并且你需要在开始时设置大 max_root - IE。 99999.

x = 64
root = 0

max_root = 99999
max_pwr = 0

for pwr in range(2,6):
    if x > 1:
        while root <= x:
            root += 1
        
            if root**pwr == x:            
                #print('root = ', root, ' and pwr = ', pwr)
                if root < max_root:
                    max_root = root
                    max_pwr = pwr
        root = 0
    elif x < 0:
        while root >= x:
            root -= 1
        
            if root**pwr == x:            
                #print('root = ', root, ' and pwr = ', pwr)
                if root < max_root:
                    max_root = root
                    max_pwr = pwr

        root = 0
    elif x == 0:
        print('There is no such pair')
        break

# after `for`-loop

print('root = ', max_root, ' and pwr = ', max_pwr)

为所有结果创建列表并在计算后过滤结果并仅显示您需要的结果的其他方法。

x = 64
root = 0

all_results = []

for pwr in range(2,6):
    if x > 1:
        while root <= x:
            root += 1
        
            if root**pwr == x:            
                #print('root = ', root, ' and pwr = ', pwr)
                all_results.append( [root, pwr] )
        root = 0
    elif x < 0:
        while root >= x:
            root -= 1
        
            if root**pwr == x:            
                #print('root = ', root, ' and pwr = ', pwr)
                all_results.append( [root, pwr] )
        root = 0
    elif x == 0:
        print('There is no such pair')
        break

# after `for`-loop

#print(all_results)

max_result = max(all_results, key=lambda x:x[0])
max_root, max_pwr = max_result
print('MAX: root = ', max_root, ' and pwr = ', max_pwr)

min_result = min(all_results, key=lambda x:x[0])
min_root, min_pwr = min_result
print('MIN: root = ', min_root, ' and pwr = ', min_pwr)