【问题标题】:How can I print only one of the inputs from a for i in range loop?如何仅打印 for i in range 循环中的一个输入?
【发布时间】:2021-10-17 08:48:20
【问题描述】:
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

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

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

所以我希望我的输入只是:

root = 8 和 pwr = 2

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

谢谢!

【问题讨论】:

  • 那么,循环内的事情会发生一次还是多次?您希望print 发生一次还是多次?因此,它应该发生在循环内部还是外部?现在,下一个问题是如何确定它应该打印什么。这里有一个提示:尝试使用一个变量来记住迄今为止看到的最佳结果。你能看到循环结束后如何使用该结果吗?
  • 不确定我是否完全理解要求,但看起来好像 print() 之后的 break 可能就是您所需要的全部
  • @BrutusForcus 是的!这解决了我的第一个问题,但现在我该如何让它只打印 root = 4 和 power = 3?
  • @KarlKnechtel 你能扩展你的提示吗?我想不出任何变量来代表最好的结果。我很欣赏第一个提示,现在为什么我要打印多个值更有意义。
  • “我想不出任何变量来代表最好的结果。”这没有任何意义。你知道什么是变量。您需要做的是为其分配一个值。你知道= 做了什么,对吧?试着一步一步地思考这个过程。用一支真正的铅笔和一张纸画一个图表。

标签: python for-loop input range


【解决方案1】:

您应该创建新变量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)

您可以为其他结果执行类似的方法,但您可能需要&lt; 而不是&gt; 行中的root &gt; max_root,并且您需要在开始时设置大max_root - 即。 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2021-06-21
    相关资源
    最近更新 更多