CodeCademy | Python | 5. Conditionals & Control flow

  1. 代码示例

    def clinic():
    print "you've just entered the clinic!"
    print "Do you take the door on the left or on the right?"
    answer = raw.input(Type left or right and hit the enter).lower()
    if answer == "left" or answer =="l":
        print "balabala..."
    elif answer =="right" or answer =="r":
        print "balabala..."
    else:
        print "balabala..."
        clinic()
    clinic()
  2. control flow: comparator
    ==(等于)、<、<=、>、>=、!=(不等于)
    注意:其中“小于等于”是“小于”或“等于”

  3. Boolean operators: and or not,真值表

    • 当程序中同时出现and or not时,程序执行的优先级是not>and>or,如:False and not False or True的结果是True;
    • 可以使用括号()按照自己的目的调整程序执行顺序,如:False or not (not True and False)的结果是True;
    • 在进行判断正误的时候,除了简单的数字比较,还对比字符串等,如“apple”!=“pear”
  4. if…elif…else

    score = raw_input("score:")
    score = int(score)
    if (score >=90) and (score <=100):
        print “A"
    elif (score >=80) and (score <90):
        print "B"
    elif (score >=70) and (score < 80):
         print "C"
    else:
        print "D"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值