栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

相关文章链接:

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

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

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

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

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循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009544.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号