【Python 编程-从入门到实践】第七章 用户输入和while循环

用户输入和 while 循环

本章中,会学习

  1. 如何接受用户输入。
  2. 如何让程序不断地运行。

7.1 函数 input()的工作原理

函数input()让程序暂停运行,等待用户输入一些文本,Python将其赋给一个变量,以方便使用。

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

函数input()接受一个参数——要向用户显示的提示(prompt)或说明,让用户知道该如何做。

在本例中,Python运行第一行代码时,用户将看到提示Tell me something, and I will repeat it back to you: 。程序等待用户输入,并在用户按回车键后继续运行。输入被赋值变量message,接下来的print(message)将输入呈现给用户。

用户输入Hello everyone!

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

7.1.1 编写清晰的程序

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

name = input("Please enter your name: ")
print(f"\nHello, {name}!")
Please enter your name: Eric

Hello, Eric!

有时候,提示可能会超过一行。可以先将提示赋给一个变量,再将该变量传递给函数input()

prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name?"

name = input(prompt)
print(f"\nHello, {name}")
If you tell us who you are, we can personalize the messages you see.
What is your first name?Eric

Hello, Eric

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

使用函数input()时,Python将用户输入解读为字符串。不能作为数来使用。

>>> age = input("How old are you?")
How old are you?21
>>> age
'21'
>>> type(age)
<class 'str'>

若试图将输入用于数值比较时,Python会引发错误,字符串无法和整数进行比较。

>>> age >= 18
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>=' not supported between instances of 'str' and 'int'

可使用函数int()来解决这个问题。函数int()将数的字符串表示转换为数值表示。

>>> type(age)
<class 'str'>
>>> age = int(age)
>>> type(age)
<class 'int'>
>>> age >= 18
True

实际上,可以使用函数int()直接读取用户输入的数值。

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

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

用户输入 71:

How tall are you, in inches? 71

You're tall enough to ride!
  1. 将数值输入用户计算和比较前,务必将其转换为数值表示。

  2. type(a)函数返回a的类型。

7.1.3 求模运算符

处理数值信息时,求模运算符(%)是个很好的工具,它将两个数相除并返回余数。

>>> 4 % 3
1
>>> 5 % 3
2
>>> 6 % 3
0
>>> 7 % 3
1

求余运算符的一个经典用法是判断一个数是奇数还是偶数。

number = input("Enter a number, and I'll tell you if it's even or odd: ")
number = int(number)

if number % 2 == 0:
    print(f"\nThe number {number} is even.")
else:
    print(f"\nThe number {number} is odd.")

用户输入 42:

Enter a number, and I'll tell you if it's even or odd: 42

The number 42 is even.

7.2 while 循环简介

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

7.2.1 使用 while 循环

可以使用while循环来数数。

current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1
1
2
3
4
5

7.2.2 让用户选择何时退出

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)

    if message != 'quit':
        print(message)
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. Hello everone!
Hello everone!

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

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

7.2.3 使用标志

使用一个变量,用于判断整个程序是否处于活动状态。这个变量称为标志(flag)

可以让程序在标志位True时继续运行,并在任何事件导致标志的值为False时让程序停止运行。这样,在while语句中就只需检查一个条件:标志的当前值是否为True

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)

标志很有用,在任意一个事件导致活动标志变成False时,循环将会退出。

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.)san francisco
I'd love to go to San Francisco!

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

在任何Python循环中都可使用break语句。

7.2.5 在循环中使用 continue

要返回循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句。

currrent_number = 0
while currrent_number < 10:
    currrent_number += 1
    if currrent_number % 2 == 0:
        continue # 不再执行下面的代码

    print(currrent_number)

打印 10 以内的所有奇数:

1
3
5
7
9

7.2.6 避免无限循环

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

x = 1
while x <= 5:
    print(x)
1
1
1
1
--snip--

若程序陷入无线循环,可按Ctrl + C或关闭显示程序输出的终端窗口来退出循环。

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

要记录大量的用户和信息,需要在while循环中使用列表和字典。

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

7.3.1 在列表之间移动元素

# 首先,创建一个待验证用户列表
#   和一个用于存储以验证用户的空列表。
unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []

# 验证每个用户,直到没有未验证用户为止。
#   将每个经过验证的用户都移到已验证用户列表中。
while unconfirmed_users:
    current_user = unconfirmed_users.pop()

    print(f"Verifying user: {current_user.title()}")
    confirmed_users.append(current_user)

# 显示所有已验证的用户。
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
    print(confirmed_user)
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confirmed:
candace
brian
alice

7.3.2 删除为特定值的所有列表元素

第三章中,使用函数remove()来删除列表中的特定值。若要删除列表中某个特定元素(有重复),可使用while循环。

pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)

while 'cat' in pets:  # 当列表中有cat则执行循环
    pets.remove('cat')

print(pets)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']

7.3.3 使用用户输入来填充字典

respones = {}

# 设置一个标志,指出调查是否继续。
polling_active = True

while polling_active:
    # 提示输入被调查者的名字和回答。
    name = input("\nWhat is your name? ")
    respone = input("Which mountain would you like to climb someday? ")

    # 将回答存储在字典中
    respones[name] = respone

    # 看看是否还有人要参与调查。
    repeat = input("Would you like to let another person respond? (yes/no) ")
    if repeat == "no":
        polling_active = False

# 调查结束,显示结果
print("\n--- Poll Results ---")
for name, respone in respones.items():
    print(f"{name} would like to climb {respone}.")

respones是一个字典,存储namerespone键值对:

What is your name? Eric
Which mountain would you like to climb someday? Denali
Would you like to let another person respond? (yes/no) yes

What is your name? Lynn
Which mountain would you like to climb someday? Devil's Thumb
Would you like to let another person respond? (yes/no) no

--- Poll Results ---
Eric would like to climb Denali.
Lynn would like to climb Devil's Thumb.

7.4 小结

在本章中,学习了:

  1. 如何在程序中使用input()来让用户提供信息;
  2. 如何处理文本和数的输入,以及如何使用while循环让程序按用户的要求不断运行;
  3. 多种控制while循环流程的方式:设置活动标志、使用break语句以及使用continue语句;
  4. 如何使用while循环在列表之间移动元素,以及如何从列表中删除所有包含特定值的元素;
  5. 如何结合使用while循环和字典。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值