【问题标题】:Is there a way to add multiple conditions in a for loop?有没有办法在 for 循环中添加多个条件?
【发布时间】:2011-10-10 03:20:06
【问题描述】:
    n=int(raw_input('enter the number of mcnuggets you want to buy : ')) #total number of               mcnuggets you want yo buy
    for a in range(1,n) and b in range(1,n) and c in range(1,n) :
        if (6*a+9*b+20*c==n):
            print 'number of packs of 6 are ',a
            print 'number of packs of 9 are ',b
            print 'number of packs of 20 are',c

我是编程新手,我正在学习 python。上面的代码给出了错误。有什么建议吗?。

【问题讨论】:

  • 错误是它给出了变量 b 没有定义。
  • 你到底想做什么?
  • “上面的代码有错误”?真的吗?什么错误?请更新问题以在代码中包含错误。请不要评论您自己的问题。请更新。
  • a in range(1,n) and b in range(1,n) and c in range(1,n) 不是“多重条件”。目前尚不清楚您的意思是什么,但不能称为多重条件。你能更新这个问题来解释你认为你想要做什么吗?
  • yossi 的回答正是我想要做的。嵌套循环。很抱歉提供了错误的信息,我是编程新手。另请告诉我如何准确更新您的问题。我找不到任何这样的选项

标签: python for-loop conditional


【解决方案1】:

你应该使用嵌套循环:

for a in range(1, n):
    for b in range(1, n):
        for c in range(1, n):
            if ...

甚至更好:

import itertools

for a, b, c in itertools.product(range(1, n + 1), repeat=3):
    if ...

【讨论】:

    【解决方案2】:

    我认为您应该从 0 开始范围,否则您将只能得到包含至少一种尺寸的答案。您还可以减少计算机的工作量,因为您知道永远不会超过n/6 需要的 6 包等。这可以节省大量资金 - 对于 45 块金块,与 97336 相比,您只需要测试 144 例

    from itertools import product
    n=int(raw_input('enter the number of mcnuggets you want to buy : ')) #total number of mcnuggets you want to buy
    for a,b,c in product(range(n//6+1), range(n//9+1), range(n//20+1)) :
        if (6*a+9*b+20*c==n):
            print 'number of packs of 6 are ',a
            print 'number of packs of 9 are ',b
            print 'number of packs of 20 are',c
    

    itertools.product 给出了 3 个范围中的 cartesian product。例如

    >>> from itertools import product
    >>> list(product(range(3),range(4),range(5)))
    [(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 3), (0, 2, 4), (0, 3, 0), (0, 3, 1), (0, 3, 2), (0, 3, 3), (0, 3, 4), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 0, 4), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 0), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 0), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 3), (2, 0, 4), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 0), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 0), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4)]
    

    【讨论】:

    • -1:很好的答案。但是,不适合在功课上苦苦挣扎的初学者。
    • 是的,实际上是关于在您回答之前我想出的范围,感谢您提供的 itertools 信息——它似乎是一个很有前途的工具。
    • @Danny,是的。 range() 确实有关于它的可选参数的不同寻常的规则。 n/6 在 Python3 中成为浮点数,n//6 始终是 int 并且在 Python2 中也可以正常工作。
    【解决方案3】:

    如果您想在for 循环中获取来自多个序列的值,则可以使用zip,例如:

    for (a,b,c) in zip(xrange(1,n), xrange(1,n), xrange(1,n)) :
        ....
    

    当然,重复相同的范围是一种浪费,但从你帖子的标题来看,我猜使用相同的范围只是一个例子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      相关资源
      最近更新 更多