[英]Iterating variable when if statement comes up false
I wrote this program in 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:
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"
Which works well, except I really don't want to have to hand iterate num
until I get the answer I want (I wrote this to solve a mathematics problem I wanted to solve - this is not homework, though). 效果很好,除了我真的不想在获得所需答案之前递给num
(我写这是为了解决我想解决的数学问题-但这不是家庭作业)。 If anyone could detail to change all of the else
statements to add a statement incrementing num
by one and return to the beginning of the for loop, that'd be great. 如果任何人都可以详细更改所有else
语句,以添加将num
递增1的语句并返回到for循环的开头,那将是很好的。
Thanks! 谢谢!
You can use the break
statement to terminate the loop 您可以使用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
What about this one? 这个如何?
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)
I think that the code's structure is wrong, you could try this instead: 我认为代码的结构是错误的,您可以尝试以下方法:
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"
The while loop tests the number on each iteration and it will exit when the number is right while循环在每次迭代中测试数字,当数字正确时,它将退出
You could clean it up by putting your checks in a function: 您可以通过将检查放入函数中来清理它:
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.