Go With the Flow

来源:互联网 发布:group by 多个字段知乎 编辑:程序博客网 时间:2024/06/14 13:52
Go With the Flow

(This course assumes familiarity with the material presented in Unit 1: Python Syntaxand Unit 2: Strings & Console Output. From here on out, take for granted that each new course assumes knowledge of the material presented in the previous courses.)

You may have noticed that the Python programs we've been writing so far have had sort of one-track minds. They compute the sum of two numbers or print something to the console, but they don't have the ability to pick one outcome over another—say, add two numbers if their sum is less than 100, or instead print the numbers to the console without adding them if their sum is greater than 100.

Control flow allows us to have these multiple outcomes and to select one based on what's going on in the program. Different outcomes can be produced based on user input or any number of factors in the program's environment. (The environment is the technical name for all the variables—and their values—that exist in the program at a given time.)

'''def clinic():    print "You've just entered the clinic!"    print "Do you take the door on the left or the right?"    answer = raw_input("Type left or right and hit 'Enter'.").lower()    if answer == "left" or answer == "l":        print "This is the Verbal Abuse Room, you heap of parrot droppings!"    elif answer == "right" or answer == "r":        print "Of course this is the Argument Room, I've told you that already!"    else:        print "You didn't pick left or right! Try again."        clinic()'''##上面采用的是递归。递归的效率比较低。改为循环def clinic():    print "You've just entered the clinic!"    print "Do you take the door on the left or the right?"    while True:        answer = raw_input("Type left or right and hit 'Enter'."    ).lower()        if answer == "left" or answer == "l":            print "This is the Verbal Abuse Room, you heap of parrot droppings!"            break        elif answer == "right" or answer == "r":            print "Of course this is the Argument Room, I've told you that already!"            break        else:            print "You didn't pick left or right! Try again."clinic()


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 蹲式厕所老是堵怎么办 下蹲式厕所堵了怎么办 蹲的厕所堵住了怎么办 厕所通了又堵怎么办 蹲式厕所经常堵怎么办 厕所下水管堵了怎么办 厕所堵了水满了怎么办 拉屎把厕所堵了怎么办 厕所堵了不下水怎么办 坐厕所堵了怎么办妙招 火车上丢了东西怎么办 网购的东西丢了怎么办 在酒店丢了东西怎么办 我好懒不想工作怎么办 被宠物刺猬咬了怎么办 被老鼠咬了怎么办打针 手指被老鼠咬了怎么办 孕妇被老鼠咬了怎么办 耳朵里面一直嗡嗡响怎么办 把语言栏删了怎么办 乐视会员到期了怎么办 预提费用取消了怎么办 小学生上课注意力不集中怎么办 工商抽查到你了怎么办 拿到商调函后该怎么办 苹果7p掉水里了怎么办 苹果7屏幕进水了怎么办 苹果5s掉进水里怎么办 苹果5s无法开机怎么办 苹果手机充不了电怎么办 苹果5s掉了怎么办 苹果6手机掉水里了怎么办 苹果5s关机丢了怎么办 苹果5s发热严重怎么办 手机开不开机了怎么办 苹果6基带坏了怎么办 苹果7基带坏了怎么办 苹果刷机1错误怎么办 4s解锁密码忘了怎么办 4sid密码忘记了怎么办 苹果4忘记id密码怎么办