天天看点

python while true循环_Python Learning-while循环

python while true循环_Python Learning-while循环

不出所料,小红的食物摊经营得越来越好,来她这里购买食物的顾客也越来越多,她简直有点应对不睱,她需要我们为她制作一个可以帮助她与顾客沟通的应用——用户可以在应用软件界面输入他们想要的食物名称,如果食物摊的食物清单里有该食物,就通知小红为将食物提供给顾客,如果食物摊的食物清单里没有该食物名称,则提示顾客这里没有他想要的食品。当然,为小红开发这样一个程序,她会付给你一笔费用的!

input()函数

input()函数可以让程序暂停运行,等待用户的输入;当用户输入完毕,并按下回车键时,input()函数将用户的输入返回,这样就可以得到顾客想要的食物名称了

food_name = input("Please tell me what kind of food you want:")

print(food_name)

首先,第一行代码会输出input()参数中的请输入您想要的食物名称:作为提示,如下

Please tell me what kind of food you want:

当用户有输入,并按下回车后,input()函数将把获得的输入信息返回,并赋值给food_name变量,然后程序接着向下执行,如下

Please tell me what kind of food you want:apple

apple

用户输入了apple,并且按下了回车,程序将用户想要的食物名称打印出来

目前只是简单的获取到了顾客想要的食物,还需要去小红的食品清单里查找是否有相应的食物

# 假设这是目前小红手头的食物清单

foods = [

{'tomato': 3, 'potato': 2, 'onion': 4},

{'apple': 5, 'banana':3.3},

{'beef': 23, 'pork': 14, 'chicken': 19.8, 'fish': 9.7}

]

# 获取用户输入的食物名称

food_name = input("Please tell me what kind of food you want:")

find_food = ""

# 从小红的食物清单里查看是否有顾客想要的食物

for food_list in foods:

if food_name in food_list.keys():

find_food = food_name

print("We have "+find_food+". Please wait a moment")

break

# 如果找到顾客想要的食物,将其赋值给变量find_food

# 如果该变量是空字符串,证明没有找到顾客想要的食物

if find_food == "":

print("Sorry, there are no "+food_name+" here")

现在,运行程序,输入apple

Please tell me what kind of food you want:apple

We have apple. Please wait a moment

程序告诉顾客,这里有苹果,请稍等

然后重新运行程序,再输入清单里没有的一种食物,如orange

Please tell me what kind of food you want:orange

Sorry, there are no orange here

程序告诉顾客,这里没有橙子

注,你可能注意到了break这个关键字,别担心,下面会详细讲解

现在,程序每次都需要重新启动,才可以再次接受顾客的输入,这实在是太笨了,如何解决?

while循环

for循环是针对于集合的一种循环,当把集合中的所有元素都遍历一遍后,for循环就会停止。而wilie循环可以无限制的循环下去,直到将指定条件改变不满足为止

while True:

do somithings

while循环通过判断条件是否为True,如果是,则一直执行,如果是False则停止循环

例:输出0~10数字

number = 0

while number <= 10:

print('\n', number)

number += 1

输出:

1

2

3

4

5

6

7

8

9

10

设置变量number的起始值为0,然后判断它是否小于等于10,如果成立,则将其输出,并在最后通过运行符+=将number的值累加一,+=效果等同于number = number + 1

break关键字让程序停止

上面的例子,是当number等于10的时候,因为执行了number += 1后,number的值已经变成了11,此时number <= 10将返回False,因此,while循环不再执行下面的代码块

number = 0

while True:

print('\n', number)

number += 1

如果现在将条件直接改为True,那么while循环每次执行判断它都是成立的,将永远执行下去,这便成了一个死循环,不仅会消耗计算机性能,还可能导致死机,这种情况一定要避免

number = 0

while True:

print('\n', number)

number += 1

if number > 10:

break

break关键字可以令循环停止,对for循环两样有效;在这里,判断如果number已经大于10,则停止循环

continue关键字

break关键字会令循环停止,而continue会让当前执行返回到循环开头,如打印0~10偶数的例子:

number = 0

while number <= 10:

if number % 2 == 0:

print("\n", number)

number += 1

输出:

2

4

6

8

10

number % 2中的%是求模运算符,即求两个数相除的余数,如果余数是0,即表示可以整除,如果余数不为0,即表示不能整除,这里通过判断number与2是否以有整除来判断它是不是一个偶数

也可以通过设置一个标志来让循环退出

# 退出标志

exit = False

number = 0

while not exit:

if number % 2 == 0:

print("\n", number)

number += 1

if number > 10:

exit = True

首先设置exit变量值为False,not exit中not表示取反的意思,not Fasle就是Ture,not Ture就是False,如果number大于10,则将exit值尽管为Ture,while循环判断not exit此时为False,因此会退出循环

所谓标志,就是自定义的一个变量,这里专门用来判断循环是否退出,此种情况适合有多种退出条件的情况

最终结果,程序可以一直监听顾客的输入,并且,当小红下班后,只要输入'quit',程序就会停止

# 退出标志

exit = False

# 假设这是目前小红手头的食物清单

foods = [

{'tomato': 3, 'potato': 2, 'onion': 4},

{'apple': 5, 'banana':3.3},

{'beef': 23, 'pork': 14, 'chicken': 19.8, 'fish': 9.7}

]

while not exit:

find_food = ""

# 获取用户输入的食物名称

txt_value = input("Please tell me what kind of food you want:")

if txt_value == 'quit':

exit = True

# 从小红的食物清单里查看是否有顾客想要的食物

for food_list in foods:

if txt_value in food_list.keys():

find_food = txt_value

print("We have "+find_food+". Please wait a moment")

break

# 如果找到顾客想要的食物,将其赋值给变量find_food

# 如果该变量是空字符串,证明没有找到顾客想要的食物

if find_food == "":

print("Sorry, there are no "+txt_value+" here")

程序运行状态:

Please tell me what kind of food you want:apple

We have apple. Please wait a moment

Please tell me what kind of food you want:

程序运行一轮后,仍旧会输出Please tell me what kind of food you want:等待用户的输入

小红兴奋的说她有了这个程序,走上人生巅峰不再是问题!

wile循环与列表、字典的结合

for循环在处理列表与字典等集合时,是无法对集合的内容进行修改的。比如在遍历一个拥有10个元素的列表时,如果在for循环体内删除掉该列表的中的一元素,则该列表的长度将减小一,而for循环本来是要循环10次的,结果到了第10次的时候发现没有,相应的位置上没有任何元素,于是会报错

wile循环在此情况下可以很好工作

例一:

# 食物清单

foods = ['tomato', 'potato', 'onion','apple', 'banana']

# 将foods中的食物名称转移到一个空列表中去,并且删除原有内容

new_foods = []

while foods:

food_name = foods.pop()

new_foods.append(food_name)

# 打印新的食物列表

print(new_foods)

# 打印旧的食物列表

print(foods)

输出:

['banana', 'apple', 'onion', 'potato', 'tomato']

[]

注:python在判断真假值时,如果判断的值是集合类型的,只要集合不为空则返回Ture,如果是空的则返回False,如果判断的是字符串,如果字符串不是空字符串,则返回Ture,如果字符串是空的则返回False

例二:

# 食物清单中有多个'potato'

foods = ['tomato', 'potato', 'onion','apple','potato', 'banana','potato']

# 删除'potato'

while 'potato' in foods:

foods.remove('potato')

# 打印食物列表

print(foods)

输出:

['tomato', 'onion', 'apple', 'banana']

'potato' in foods判断'potato'元素是否包含在列表foods中,如果包含则返回True

remove('potato')每次只会删除匹配到的第一个元素,也就是一次只能删除一个