python编程从入门到实践第七章——用户输入和while循环

相关文章链接:

python编程从入门到实践第二章——变量和简单数据类型

python编程从入门到实践第三章——列表简介

python编程从入门到实践第四章——操作列表

python编程从入门到实践第五章——if语句

python编程从入门到实践第六章——字典

python编程从入门到实践第八章——函数​​​​​​​

python编程从入门到实践第九章——类​​​​​​​

python编程从入门到实践第十章——文件和异常​​​​​​​

7.1 函数input()的工作原理

函数input()让程序暂停运行,等待用户输入一些文本。

message = input("Tell me something, and I will repeat it back to you:")
print(message)

#Tell me something, and I will repeat it back to you: Hello everyone!
#Hello everyone!

7.1.1 编写清晰的程序

每当使用函数input()时,都应指定清晰易懂的提示,准确的指出希望用户提供什么样的信息。

7.1.2 使用int()来获取数值输入

使用input()时,Python将用户输入解读为字符串。试图将输入用于数值比较时,python会引发错误,因为它无法将字符串和整数进行比较。

可使用函数int(),它让Python将输入视为数值。

height = input("How tall are you, in inches?")
height = int(height)

if height >= 48:
    print("\nYou're tall enough to ride!")
else:
    print("\nYou're be able to ride when you're a little older.")

#How tall are you, in inches? 71
#You're tall enough to ride!

在比较前,height = int(height)将输入转换成了数值表示。

7.1.3 求模运算符

处理数值信息时,求模运算符(%很有用,它将两个数相除并返回余数

求模运算不会指出一个数是另一个数的多少倍,只指出余数是多少。

4%3
#1

5%3
#2

6%3
#0

7%3
#1

7.2 while循环简介

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

7.2.1 使用whike循环

current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1

#1
#2
#3
#4
#5

7.2.2 让用户选择何时退出

可以使用while循环让程序在用户愿意时不断运行,我们在其中定义了一个退出值,只要用户输入的不是这个值,程序就将接着运行:

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program"
message = ''
while message != 'quit':
    message input(prompt)
    print(message)

#Tell me something, and I will repeat it back to you:
#Enter 'quit' to end the program.Hello everyone!
#Hello everyone!

#Tell me something, and I will repeat it back to you:
#Enter 'quit' to end the program.quit
#quit

7.2.3 使用标志

在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。这个变量称为标志(flag),充当程序的交通信号灯。可以让程序在标志为True时继续运行,并在任何事件导致标志的值为False时让程序停止运行。

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program"

active = True
while active:
    message = input(prompt)
    if message == 'quit':
        active = False
    else:
        print(message)

7.2.4 使用break退出循环

要立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句。break语句用于控制程序流程,可用来控制哪些代码将执行、哪些代码行不执行,从而让程序按你的要求执行你要执行的代码。

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.)"

while True:
    city = input(prompt)
    
    if city == 'quit'
        break
    else:
        print(f"I'd love to go to {city.title()}!")

# Please enter the name of a city you have visited:
#(Enter 'quit' when you are finished.)New York
#I'd love to go to New York

## Please enter the name of a city you have visited:
#(Enter 'quit' when you are finished.)quit

 注意:在任何python循环中都可以使用break语句。

7.2.5 在循环中使用continue

要返回循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,它不像break语句那样不再执行余下的代码并退出整个循环。

current_number = 0
while current_number < 10:
    current_number +=1
    if current_number % 2 == 0:
        continue
    print(current_number)

#1
#3
#5
#7
#9

7.2.6 避免无限循环

每个while循环都必须要有停止运行的途径,这样才不会没完没了地执行下去。

7.3 使用while循环处理列表和字典

for循环是一种遍历列表的有效方式,但不应在for循环中修改列表,否则将导致python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环。通过将while循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白天数糖晚上数羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值