2016-08-16 34 views
0

我在Python写了这个程序:迭代变量时,if语句出现假

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语句以添加一条语句,然后返回到for循环的开头,那么这会很棒。

谢谢!

+0

@heather将被视为交叉发布,WHI ch皱起了眉头。 – Mast

+2

@JamesK:可以说,它实际上并不“工作”,因为你必须多次运行它。她并没有要求进行代码审查。 – recursive

+1

@JamesK这将是一个糟糕的Code Review。保持它。 – Mast

回答

2

可以使用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 
+0

谢谢!这是一个很好的解决方案,也是我最理解的解决方案,这就是我接受它的原因。 – heather

+0

@heather:谢谢。我认为这里的大部分解决方案都适合一般用途,但我认为这种方法对于初学者来说可能是最容易接受的。 – recursive

+0

那么,我绝对是一个初学者,你的方法是赞赏。再次感谢! – heather

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) 
+0

谢谢,这是一个很好的解决方案! – heather

+0

@heather不客气,我已经为了你自己的缘故添加了一个仁慈的蛋,因为这不是功课,而且看起来你喜欢数学:) – BPL

+0

确实我喜欢数学(和编码)。再次感谢你的帮助! – heather

1

我认为代码的结构是错误的,你可以改为尝试这个办法:

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循环测试在每次迭代次数和时数是正确

+0

谢谢,这是另一个很好的解决方案! – heather

0
将退出

你可以把你的支票在函数清理:

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