有没有一种方法可以在for循环中添加多个条件? [英] Is there a way to add multiple conditions in a for loop?

查看:581
本文介绍了有没有一种方法可以在for循环中添加多个条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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。上面的代码给出了错误。有任何建议吗??

i am new to programming and i am learning python.the code above gives errors. Any suggestion.?.

推荐答案

您应该使用嵌套循环:

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 ...

这篇关于有没有一种方法可以在for循环中添加多个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆