Home > Back-end >  How can I print only one of the inputs from a for i in range loop?
How can I print only one of the inputs from a for i in range loop?

Time:10-17

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

The inputs I receive are: root = 8 and pwr = 2 root = 4 and pwr = 3

However, I am only looking to get the highest root answer if available.

So what I want my input to be is only:

root = 8 and pwr = 2

And also vice versa... what should I do if I only want it to display: root = 4 and pwr = 3

Thanks!

CodePudding user response:

You should create new variables max_root, max_pwr and when you calculate root then you should check if it is bigger then max_root and replace max_root, max_pwr or keep older value.

And you should display only after all calculations

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)

Similar way you can do for other result but you may need < instead of > in line root > max_root and you need to set big max_root at start - 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)

Other method to create list for all results and after calculations filter results and display only results which you need.

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)
  • Related