文章目录

  • 前言
  • 一、基本内容
  • 1.input()函数
  • 2.while循环
  • 3.while循环处理列表和字典
  • 二、代码
  • 总结



前言

大多数的程序都需要从用户那里获得一些信息,这一章主要是学习input()函数。
为了让用户输入信息,并在程序中使用这些信息,就需要用到while循环。


一、基本内容

1.input()函数

函数input(),让程序暂停,等待用户输入一些文本信息,获得文本信息后,python将这些信息存储在一个变量中,方便后续使用。

函数input()接受一个参数,即是向用户显示的说明。若是为字符串变量,别忘了加上引号。用户输入完成后,按回车程序继续运行。当提示比较长,或者跨越几行时,可以将提是信息存储在一个变量中。

注意:input()函数获得的是字符串变量,所以当对数字进行比较大小时,需要先将字符串变量转换成数字变量。一般用int()函数。

2.while循环

for循环是针对集合中每一个元素都循环的代码块,而while循环是不断的运行,直到循环条件不满足为止。

在使用while循环时,循环条件我们一般定义为一个布尔类型的变量,称为标志,当这个标志为true时while循环,若想退出循环,只需要将这个变量改为false即可,这种情况下,while循环会先执行完剩下的代码,然后再退出循环。

但当我们想要满足某个条件,立刻停止循环,就需要用到break语句。

continue语句是,跳过当前的循环,查看下一次循环条件是否满足,若满足就进行下一次循环。

使用while循环时特别注意的一点就是一定要避免无限循环。

3.while循环处理列表和字典

for循环是一种遍历列表的有效方式,但他不适合用于修改列表,若要修改列表,我们可以使用while循环。

在列表间移动元素:循环条件可以指定为某个列表为空,然后用pop()函数栈出,后append()或者insert()即可。

删除包含特征值的所有列表元素:remove()函数只能删除一次最先出现的特征值,若想删掉全部,就需要结合while循环,循环条件一般用in语句。

用户输入来填充字典:字典的每一个元素由两部分组成,一个是键,一个是值,所以在填充字典时,一般会有成双出现,其中一个输入作为键,一个作为值。填充字典的方法使用赋值的方法就行。

二、代码

1.汽车租赁:输入租赁的汽车,并返回一条信息。
2.餐馆订位:询问有多少人,超过八人,桌椅不够
3.10的整数倍:输入一个数字,让程序验证是否为10的整数倍
4.披萨配料:提是用户输入披萨配料,在输入quit 时退出程序,打印一条消息
5.电影票:询问用户的年龄并打印其票价
6.三个出口,用另一种方法完成练习4与5,使用while active 与break,
7.编写一个无限循环。
8.熟食店,将订单列表元素转移到完成列表中,并打印
9.牛肉卖完了,用一个while循环将订单里的牛肉给剔除
10.梦想的度假胜地,用户及它的度假胜地。

#租赁汽车
from calendar import TUESDAY
from math import fabs
from os import remove


car=input('which car do you want to lent?')
print(car)
#餐馆订位
res=input('how many people there is?')
if int(res)>8:
    print('the room is not enough')
else:
    print('the room is enough')
#10的整数倍
num=input('please input a number')
if int(num)%10==0:
    print ('该数字是10的整数倍')
else:
    print('该数字不是10的整数倍')
#披萨配料
while True:
    ingredient=input('input "quit" to stop ,please input pizza ingredient:')
    if ingredient=='quit':
        break
    else:
        print('we will add this ingredient')
#电影票
while True:
    age=input('please input your age: ')
    if int(age)<3:
        print('free')
        break
    elif int(age)>3 and int(age)<12:
        print('10 yuan')
        break
    elif int(age)>12:
        print('12 yuan')
        break
#三个出口
active=True
while active:
    age=input('please input your age again: ')
    if int(age)<3:
        print('free')
        active=False
    elif int(age)>3 and int(age)<12:
        print('10 yuan')
        active=False
    elif int(age)>12:
        print('12 yuan')
        active=False
#熟食店
sandwich_order=['beef sandwich','cheese sandwich','ham sandwich']
sandwich_finished=[]
while len(sandwich_order):
    temp=sandwich_order.pop()
    sandwich_finished.append(temp)
    print('I made your '+temp)

print(sandwich_finished)
#ham sandwich 卖完了
print('ham sandwich is not available')
sandwich_order=['beef sandwich','cheese sandwich','ham sandwich','ham sandwich','ham sandwich']
sandwich_finished=[]
while 'ham sandwich' in sandwich_order:
    sandwich_order.remove('ham sandwich')
if 'ham sandwich' not in sandwich_order:
    print('there is no ham sandwich order')
#喜欢的地方
responses={}
active_1=True
while active_1:
    name=input('\nwhat is your name: ')
    respond=input('\nwhich place would you like')
    responses[name]=respond
    repeat=input('\nwould you like to let another person respond?(yes/no)')
    if repeat=="no":
        active_1=False
for key,value in responses.items():
    print(key+':'+value)

结果如下

which car do you want to lent?bus
bus
how many people there is?9
the room is not enough
please input a number10
该数字是10的整数倍
input "quit" to stop ,please input pizza ingredient:quit
please input your age: 5
10 yuan
please input your age again: 99
12 yuan
I made your ham sandwich
I made your cheese sandwich
I made your beef sandwich
['ham sandwich', 'cheese sandwich', 'beef sandwich']
ham sandwich is not available
there is no ham sandwich order

what is your name: zhangsan

which place would you likejinan

would you like to let another person respond?(yes/no)no
zhangsan:jinan

总结

本章学习了:1.input函数
2.while语句以及终止while语句的许多办法