我的python旅程——选择与循环
作业描述
作业解答
(1)第一种
x=1
while 1:
if x%2==1 and x%3==0 and x%4==1 and x%5==4 and x%6==3 and x%7==0 and x%8==1 and x%9==0:
print("There are {} candies in this box".format(x))
break
else:
x=x+1
答案
There are 1449 candies in this box
!!!利用while计数与break跳出循环的功能
(2)第二种
循环枚举直至桃子/糖果数目(num)满足所有条件表达式
num = 1
while not(num%2==1 and num%3==0 and num%4==1 and num%5==(5-1) and num%6==3 and num%7==0 and num%8==1 and num%9==0):
num += 1
else:
print("There are {} candies in this box.".format(num))
Output:
There are 1449 candies in this box.