python大大的图

我的图

在复杂的程序中很多不同的事件都会导致程序停止运行,如果在循环中要检查这么多变量就会导致循环的编写变得复杂。这时候我们可以定义一个变量用于判断整个程序是否处于活动状态,这个变量为True时程序继续运行,而为False时程序停止运行。这个变量被称为标志。而标志处于True还是False,由程序其他部分处理。
在while循环里,continue用于跳出该次循环,如果满足循环条件会再次进入while循环。break用于跳出整个while循环。
如果程序陷入无限循环,可按Ctrl+C,也可关闭显示程序输出的终端窗口。

按Ctrl+C之后无限循环停止了

比萨配料:编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit'时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料

而这题如果把输入放在循环外就会变成无限循环

还是这个程序,在Spyder上用Ctrl+C停止时,是这样的

在这个程序中加入三个要求:(1)在while循环中使用条件测试来结束循环。(2)使用变量active来控制循环结束的时机。(3)使用break语句在用户输入'quit'时退出循环。


电影票:有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。

严格限定年龄范围可以防止用户输入错误的数据。如果输入年龄为-1,循环退出。
这是一个票价查询程序,如果要设置成购票程序就需要加入票数进行计算。假设一对30岁的爸爸妈妈带着一个2岁,一个5岁的孩子去看电影

很明显这个程序有bug,年龄只能由小到大,而不能任意输入。我暂时还没想到更好的写法,会写的小伙伴教我一下哟~
网友评论