我的python旅程——选择与循环

我的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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值