Python if语句出现false时迭代变量

Python if语句出现false时迭代变量,python,Python,我用python编写了这个程序: num=51 if (num % 3 == 1): if (num%4 == 2): if (num%5 == 3): if (num%6 ==4): print num else: print "not right number, try again - error 1" else:

我用python编写了这个程序:

num=51

if (num % 3 == 1):
    if (num%4 == 2):
        if (num%5 == 3):
            if (num%6 ==4):
                print num
            else:
                print "not right number, try again - error 1"
        else:
            print "not right number, try again - error 2"
    else:
        print "not right number, try again - error 3"
else: 
    print "not right number, try again - error 4"
这很管用,除了我真的不想在得到我想要的答案之前手动迭代
num
(我写这篇文章是为了解决我想解决的数学问题,但这不是家庭作业)。如果有人可以详细更改所有
else
语句,添加一个以1递增
num
的语句并返回for循环的开头,那就太好了


谢谢

您可以使用
break
语句终止循环

num=1

while True:
    if (num % 3 == 1):
        if (num%4 == 2):
            if (num%5 == 3):
                if (num%6 ==4):
                    print num
                    break
                else:
                    print "not right number, try again - error 1"
            else:
                print "not right number, try again - error 2"
        else:
            print "not right number, try again - error 3"
    else: 
        print "not right number, try again - error 4"
    num += 1

您可以使用
break
语句终止循环

num=1

while True:
    if (num % 3 == 1):
        if (num%4 == 2):
            if (num%5 == 3):
                if (num%6 ==4):
                    print num
                    break
                else:
                    print "not right number, try again - error 1"
            else:
                print "not right number, try again - error 2"
        else:
            print "not right number, try again - error 3"
    else: 
        print "not right number, try again - error 4"
    num += 1
这个怎么样

def f(n):
    for (a, b) in [(3, 1), (4, 2), (5, 3), (6, 4)]:
        if(num % a) != b:
            return (False, b)

    return (True, n)

for num in range(100):
    print '-' * 80
    v = f(num)

    if not v[0]:
        print "{0} is not the right number, try again - error {1}".format(num, v[1])
    else:
        print "The first number found is --> {0}".format(v[1])
        break


N = 1000000
numbers = [num for num in range(N) if f(num)[0]]
print "There are {0} numbers satisfying the condition below {1}".format(
    len(numbers), N)
这个怎么样

def f(n):
    for (a, b) in [(3, 1), (4, 2), (5, 3), (6, 4)]:
        if(num % a) != b:
            return (False, b)

    return (True, n)

for num in range(100):
    print '-' * 80
    v = f(num)

    if not v[0]:
        print "{0} is not the right number, try again - error {1}".format(num, v[1])
    else:
        print "The first number found is --> {0}".format(v[1])
        break


N = 1000000
numbers = [num for num in range(N) if f(num)[0]]
print "There are {0} numbers satisfying the condition below {1}".format(
    len(numbers), N)

我认为代码的结构是错误的,您可以尝试以下方法:

num=51

def test(num):
    # keep all the tests in a list
    # same as tests = [num % 3 == 1, num % 4 == 2, ...]
    tests = [num % x == y for x,y in zip(range(3,7), range(1,5))]

    if all(tests):    # if all the tests are True
        return False  # this while exit the loop 
    else:
        # message to be formatted
        msg = "{n} is not the right number, try again - error {err}"

        # I tried to keep your error numbers
        err = len(tests) - tests.count(False) + 1

        # format the message with the number and the error
        print msg.format(n=num, err=err)

        return True

while test(num):
    num += 1  # increment the number

print num, "is the right number"

while循环在每次迭代中测试数字,当数字正确时它将退出

我认为代码的结构是错误的,您可以尝试以下方法:

num=51

def test(num):
    # keep all the tests in a list
    # same as tests = [num % 3 == 1, num % 4 == 2, ...]
    tests = [num % x == y for x,y in zip(range(3,7), range(1,5))]

    if all(tests):    # if all the tests are True
        return False  # this while exit the loop 
    else:
        # message to be formatted
        msg = "{n} is not the right number, try again - error {err}"

        # I tried to keep your error numbers
        err = len(tests) - tests.count(False) + 1

        # format the message with the number and the error
        print msg.format(n=num, err=err)

        return True

while test(num):
    num += 1  # increment the number

print num, "is the right number"

while循环在每次迭代中测试数字,当数字正确时它将退出

您可以通过将检查放入函数中来清除它:

def good_number(num):
    if num % 3 == 1:
        if num % 4 == 2:
            if num % 5 == 3:
                if num % 6 == 4:
                    return True
    # Put your elses/prints here

# Replace 100 with your max
for num in range(100): 
    if good_number(num):
        print('{} is a good number'.format(num))

# Or use a while loop:
num = 0
while not good_num(num):
    num += 1

print('{} is a good number'.format(num))

您可以通过将支票放在函数中来清理它:

def good_number(num):
    if num % 3 == 1:
        if num % 4 == 2:
            if num % 5 == 3:
                if num % 6 == 4:
                    return True
    # Put your elses/prints here

# Replace 100 with your max
for num in range(100): 
    if good_number(num):
        print('{} is a good number'.format(num))

# Or use a while loop:
num = 0
while not good_num(num):
    num += 1

print('{} is a good number'.format(num))


@希瑟:这会被认为是交叉发布,这是不受欢迎的。@JamesK:可以说,它实际上并不“有效”,因为你必须运行多次。她没有要求代码审查。@JamesK这太不适合代码审查了。别管它了,好吧,反正我有答案。谢谢大家的帮助@希瑟:这会被认为是交叉发布,这是不受欢迎的。@JamesK:可以说,它实际上并不“有效”,因为你必须运行多次。她没有要求代码审查。@JamesK这太不适合代码审查了。别管它了,好吧,反正我有答案。谢谢大家的帮助!谢谢,这是一个很好的解决方案@希瑟:不客气,我为你自己加了一个更友善的鸡蛋,只是因为这不是家庭作业,而且你似乎喜欢数学:)我确实喜欢数学(和编码)。再次感谢你的帮助@希瑟我也是,这纯粹是乐趣。。。试一试;)谢谢,这是一个很好的解决方案@希瑟:不客气,我为你自己加了一个更友善的鸡蛋,只是因为这不是家庭作业,而且你似乎喜欢数学:)我确实喜欢数学(和编码)。再次感谢你的帮助@希瑟我也是,这纯粹是乐趣。。。试一试;)非常感谢。这是一个很好的解决方案,也是我最了解的,这就是我接受它的原因。@heather:谢谢。我认为这里的大多数其他解决方案更适合一般使用,但我认为这种方法对初学者来说可能是最容易接近的。嗯,我肯定是个初学者,你的方法很受欢迎。再次感谢!非常感谢。这是一个很好的解决方案,也是我最了解的,这就是我接受它的原因。@heather:谢谢。我认为这里的大多数其他解决方案更适合一般使用,但我认为这种方法对初学者来说可能是最容易接近的。嗯,我肯定是个初学者,你的方法很受欢迎。再次感谢!