一.函数input()的工作原理
1.1input接收字符串
函数input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用。
message=input("Please input your name:")
print(message);
函数input()接受一个参数:即要向用户显示的提示或说明,让用户知道该如何做。在上面示例中,python运行第一行代码时,用户将看到提示:“Please input your name:”。程序暂停运行等待用户输入,并在用户按回车键后继续运行。输入存储在变量message中,然后print(message)将输入呈现给用户。
运行结果
1.2使用int()来获取数值输入
在使用函数input()时,pyhon将用户输入的解读为字符串。因此在我们需要接收用户传输过来的数字时,需要对接收的内容进行类型转换。
age=input("Please tell me your age:")
age=int(age)
if age>=18:
print("\n 你是成年人!")
else:
print("\n 未成年禁止入内!")
1.3求模运算符
处理数值信息时,求模运算符(%)是一个很有用的工具,他将两个数相除,并返回余数
>>> 4 % 3
1
>>> 5 % 3
2
>>> 6 % 3
0
>>> 7 % 3
1
求模运算符不会指出一个数是另一个数的多少倍,而只指出余数是多少。如果一个数可被另一个数整除,余数就为0,因此求模运算符将返回0.可利用这一点来判断偶数和奇数
number=input("请输入一个数字:")
number=int(number)
if number%2==0:
print("\n 你输入的是偶数!")
else:
print("\n 你输入的是奇数!")
随堂测试:
汽车租赁 :编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如“你要租赁的汽车是跑车”。
carType=input("请输入你要租赁的汽车:")
print("你要租赁的汽车是"+carType+"!!!")
餐馆订位 :编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌。
theNumber=input("请输入用餐人数:")
message="您的用餐人数为"+theNumber+"人,"
theNumber=int(theNumber)
if theNumber>8:
message+="暂无空桌,请稍后!!!"
else:
message+="有空桌。"
print(message)
10的整数倍 :让用户输入一个数字,并指出这个数字是否是10的整数倍。
theNumber=input("请输入一个数字:")
theNumber=int(theNumber)
if theNumber%10==0:
print("是10的倍数")
else:
print("不是10的倍数")
二.while循环简介
1.1使用while循环
for循环用于针对集合中的每个元素都有一个代码块。而while循环不断地运行,直到指定的条件不满足为止。
number=1
while number<=5:
print(number)
number+=1
1.2让用户选择何时退出
prompt="请输入要打印的信息:"
prompt+="(输入esc退出)"
message=""
while message!='esc':
message=input(prompt)
if message!='esc':
print(message)
1.3使用标志
在要求很多条件都满足才继续运行的程序中,可以定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志,充当了程序的交通信号灯。你可以让程序在标志为True的时候继续运行,并在任何标志的值为false时让程序停止。这样,在while语句中只需要检查一个条件-----标志当前是否为True.
prompt="请输入要打印的信息:"
prompt+="(输入esc退出)"
active=True
while active:
message=input(prompt)
if message=='esc':
active=False
else:
print(message)
1.4使用break退出循环
要立即退出while循环,不再运行循环中余下的代码,不管条件测试的结果如何,可以使用break语句。break用于控制程序流程,可以控制哪些代码执行,哪些代码不执行,从而让程序按你的要求执行你要执行的代码
prompt="请输入要打印的信息:"
prompt+="(输入esc退出)"
while True:
message=input(prompt)
if message=='esc':
break
else:
print(message)
以while True打头的循环将不断运行,直到遇到break。
注:在任何python循环中都可以使用break语句。例如,可以使用break语句来退出遍历列表或字典的for循环
1.5在循环中使用continue
要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,他不像break语句那样不再执行余下代码并退出整个循环。
number=0
while number<10:
number+=1
if number%2==0:
continue
print(number)
运行结果
1.6避免无限循环
每一个while循环都必须有停止运行的途径,这样才不会没完没了的执行下去。
随堂测试
1.比萨配料:编写一个循环,提示用户输入一系列的比萨配料,并在用户输入‘quit’时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
1-a
message="请输入比萨配料:"
message+="(输入'quit'完成)"
inputMessage=""
while inputMessage!="quit":
inputMessage=input(message)
print(inputMessage)
1-b
message="请输入比萨配料:"
message+="(输入'quit'完成)"
active=True
while active:
inputMessage=input(message)
if inputMessage=='quit':
active=False
else:
print(inputMessage)
1-c
message="请输入比萨配料:"
message+="(输入'quit'完成)"
while True:
inputMessage=input(message)
if inputMessage=='quit':
break
else:
print(inputMessage)
2.电影票:有家电影院根据观众的年龄收取不同的票价:不到三岁的观众免费;3-12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出票价。
2-a
message="请输入您的年龄:"
breakMesage="要退出吗?(yes/no)"
inputMessage=""
while inputMessage!="yes":
age=input(message)
printMessage="您的年龄为"+age+"岁,"
age=int(age)
if age<3:
print(printMessage+"可免费观看")
if 3<=age<=12:
print(printMessage+"票价为10美元")
if age>12:
print(printMessage+"票价为15美元")
inputMessage=input(breakMesage)
2-b
message="请输入您的年龄:"
breakMesage="要退出吗?(yes/no)"
active=True
while active:
age=input(message)
printMessage="您的年龄为"+age+"岁,"
age=int(age)
if age<3:
print(printMessage+"可免费观看")
if 3<=age<=12:
print(printMessage+"票价为10美元")
if age>12:
print(printMessage+"票价为15美元")
inputMessage=input(breakMesage)
if inputMessage=="yes":
active=False
3-c
message="请输入您的年龄:"
breakMesage="要退出吗?(yes/no)"
while True:
age=input(message)
printMessage="您的年龄为"+age+"岁,"
age=int(age)
if age<3:
print(printMessage+"可免费观看")
if 3<=age<=12:
print(printMessage+"票价为10美元")
if age>12:
print(printMessage+"票价为15美元")
inputMessage=input(breakMesage)
if inputMessage=="yes":
break
3.三个出口:以另一种方式完成练习1,2在程序采取以下做法
a.在while循环中使用条件测试来结束循环(答案见1-a,2-a)
b.使用变量active来控制循环结束的时机(答案见1-b,2-b)
c.使用break语句在用户输入‘quit’时退出循环(答案见1-c,2-c)
4.无限循环:编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl+C,也可关闭显示输出的窗口)。
while True:
aaa=1
print(aaa)
三.使用while循环来处理列表和字典.
for循环是一种遍历列表的有效方式,但在for循环中不应该修改列表,否则将导致Python难以追踪其中的元素。要在遍历列表的同事对其进行修改,可使用while循环。