python输入end退出循环_Python 学习笔记 - 用户输入和while循环

本文介绍了Python的input()函数及其使用,讲解了如何处理用户输入的数值并进行比较,详细阐述了while循环的工作原理及如何退出循环,包括通过用户输入特定值、设置标志变量和使用break语句。同时,文章还提到了continue语句在循环中的应用以及while循环在处理列表和字典中的用法。
摘要由CSDN通过智能技术生成

1.用户输入函数 input() 的工作原理

函数 input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用。

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

Tell me something, and I will repeat it back to you: hello Python!

>>> print(message)

hello Python!

当你使用 input() 时,应该指定清晰而易于明白的提示,准确地指出你希望用户提供什么样的信息。

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

使用函数 input() 时,Python将用户输入解读为字符串,所以一旦你想用用户输入的数据来跟其他类型比较时,就必须做类型转换。

>>> age = input("How old are you ?")

How old are you ?30

>>> age

'30'

如上所示我们得到了一个 30 的字符串。

>>> age >= 30

Traceback (most recent call last):

File "", line 1, in

age >= 30

TypeError: '>=' not supported between instances of 'str' and 'int'

当我们做数值比较时报错。类型不匹配。所以这里就要用到 int() 函数将得到的字符串转换为 int 类型:

>>> int(age) >= 30

True

3.求模运算符(%)

用来处理数值信息,它能将两个数相除并返回余数:

>>> 4 % 3

1

>>> 5 % 3

2

>>> 6 % 3

0

>>> 7 % 3

1

可以用求模运算来判断一个数是奇数还是偶数。

4.while 循环

for 循环用于针对集合中的每个元素的一个代码块(循环次数根据指定集合中对象个数来判断),而 while 循环不断地运行,直到指定的条件不满足为止。编写 while 循环时注意循环条件,不要写了一个无限循环,哈哈。

语法:

while 循环条件:

执行逻辑

4.1.while 循环的退出

让用户自己选择退出:

>>> prompt = "\n Tell 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 world!

hello world!

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

Enter 'quit' to end the program.quit

quit

使用标志:

>>> active = True

>>> while active:

message = input(prompt)

if message == 'quit':

active = False

else:

print(message)

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

Enter 'quit' to end the program.hello world!

hello world!

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

Enter 'quit' to end the program.quit

使用 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("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.)Beijing

I'd love to go to Beijing!

Please enter the name of a city you have visited.

(Enter 'quit' when you are finished.)quit

4.2.循环中的 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

4.3.while 循环在 列表 和 字典 中的应用

循环列表,在循环列表时必须有个结束的逻辑,比如使用 pop() 函数或者 break 语句,否则进入无限循环。

>>> users = ['alice', 'brian', 'candace']

>>> while users:

print(users.pop())

candace

brian

alice

循环列表 users,依次移除列表 users 末尾的元素。直到最后一个元素被移除,循环结束。

>>> pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']

>>> print(pets)

['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']

>>> while 'cat' in pets:

pets.remove('cat')

>>> print(pets)

['dog', 'dog', 'goldfish', 'rabbit']

循环列表 pets,循环移除元素 'cat' 直到 'cat' 全部移除 pets 中,循环结束。

循环字典

>>> responses = {}

>>> polling_active = True

>>> while polling_active:

name = input("\n What is your name?")

reponse = input("Which mountain would you like to climb someday?")

responses[name] = reponse

repeat = input("Would you like to let another person respond?(yes/no)")

if repeat =='no':

polling_active = False

What is your name?dylan

Which mountain would you like to climb someday?Tai

Would you like to let another person respond?(yes/no)no

>>> for name, response in responses.items():

print(name + " would like to climb " + response + '.')

dylan would like to climb Tai.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值