摘要:本章主要介绍如何进行用户输入,循环,以及与循环配合使用的语句。函数在中,使用函数获取用户输入,这里请注意的返回值为字符串。值得提醒的是,编写循环时应避免死循环,或者叫做无限循环,比如循环忘记了变量自增。
《Python编程:从入门到实践》笔记。1. input() 函数
本章主要介绍如何进行用户输入,while循环,以及与循环配合使用的break, continue语句。
在Python中,使用input()函数获取用户输入,这里请注意:input()的返回值为字符串。如果输入的是数字,并且要用于后续计算,需要进行类型转换。
input()函数可以传入字符串参数作为输入提示,如下:
# 代码:
number = input()
# 判断数据类型的两种方法
print(type(number))
print(isinstance(number, str))
print(int(number) ** 2) # int()函数将字符串转换成整数
# 如果提示超过一行,可以将提示放在变量中,再将变量传入input();
# 并且最好在提示后面留一个空格以区分提示和用户输入
message = input("Tell me something, and I will repeat it back to you: ")
print(message)
# 结果:
123
True
15129
Tell me something, and I will repeat it back to you: Hello, everyone!
Hello, everyone!
判断奇偶(作为对前文常见运算的补充):取模运算%,返回余数
# 代码:
number = input("Enter a number, and I"ll tell you if it"s even or odd: ")
number = int(number)
if number % 2:
print("
The number " + str(number) + " is even.")
else:
print("
The number " + str(number) + " is odd.")
# 结果:
Enter a number, and I"ll tell you if it"s even or odd: 123
The number 123 is even.
2. while 循环简介
for循环用于针对集合中的每个元素的一个代码块,而while循环不断地运行,直到指定的条件不满足为止。比如,让用户选择何时退出:
# 代码:
prompt = "
Tell me something, and I will repeat it back to you:"
prompt += "
Enter "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 everyone!
Hello everyone!
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
2.1 使用标志
在上述代码中我们直接对输入数据进行判断,这样做在简单的程序中可行,但复杂的程序中,如果有多个状态同时决定while循环的继续与否,要是还用上述的方法,则while循环的条件判断将很长很复杂,这时可以定义一个变量作为标志来代替多个条件。使用标志来改写上述代码:
prompt = "
Tell me something, and I will repeat it back to you:"
prompt += "
Enter "quit" to end the program. "
active = True
while active:
message = input(prompt)
if message != "quit":
active = False
else:
print(message)
在复杂的程序中,如很多事件都会导致程序停止运行的游戏中,标志很有用:在其中的任何一个事件导致活动标志变为False时,主游戏循环将退出。
2.2 使用break退出循环要立即退出while或者for循环,不在执行循环中余下的代码,也不管条件测试的结果如何,可使用break语句。再将上述使用标志的代码改写为break:
prompt = "
Tell me something, and I will repeat it back to you:"
prompt += "
Enter "quit" to end the program. "
while True:
message = input(prompt)
if message != "quit":
break
print(message)
2.3 在循环中使用continue
如果满足某条件时要返回循环开始处,而不是跳出循环,则使用continue语句。以下是打印1到10中的所有奇数的代码:
# 代码:
count = 0
while count < 10:
count += 1
if count % 2 == 0:
continue
print(count)
# 结果:
1
3
5
7
9
break与continue的区别:break跳过循环体内余下的所有代码,并跳出循环;continue跳过循环体内余下的所有代码,回到循环体开始处继续执行,而不是跳出循环体。
值得提醒的是,编写循环时应避免死循环,或者叫做无限循环,比如while循环忘记了变量自增。
将未验证用户经验证后变为已验证用户:
# 代码:
unconfirmed_users = ["alice", "brian", "candace"]
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("Verifying user: " + current_user.title())
confirmed_users.append(current_user)
print("
The following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
# 结果:
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice
The following users have been confirmed:
Candace
Brian
Alice
3.2 删除包含特定值的所有列表元素
之前的章节中使用remove()函数来删除列表中的值,但只删除了列表中的第一个指定值,以下代码循环删除列表中指定的值:
# 代码:
pets = ["dog", "cat", "dog", "goldfish", "cat", "rabbit", "cat"]
print(pets)
while "cat" in pets:
pets.remove("cat")
print(pets)
# 结果:
["dog", "cat", "dog", "goldfish", "cat", "rabbit", "cat"]
["dog", "dog", "goldfish", "rabbit"]
3.3 使用用户输入来填充字典
# 代码:
responses = {}
# 设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答
name = input("
What is your name? ")
response = input("Which mountain would you like to climb someday? ")
# 将回答存入字典
responses[name] = response
# 是否还有人要参与调查
repeat = input("World you like to let another person respond?(yes/ no) ")
if repeat == "no":
polling_active = False
# 调查结束,输出结果
print("
--- Poll Results ---")
for name, response in responses.items():
print(name + " world like to climb " + response + ".")
# 结果:
What is your name? Eric
Which mountain would you like to climb someday? Denali
World 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
World you like to let another person respond?(yes/ no) no
--- Poll Results ---
Eric world like to climb Denali.
Lynn world like to climb Devil"s Thumb.
迎大家关注我的微信公众号"代码港" & 个人网站 www.vpointer.net ~
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/41796.html
摘要:是个的一种实现方式,编译代码为字节码,然后由虚拟机执行,这意味着此时程序与程序没有区别,只是源代码不一样。原文链接全栈之路系列文章 Python的诞生 Python是著名的龟叔Guido van Rossum(吉多·范罗苏姆)在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言。 showImg(https://segmentfault.com/img/remote/146...
摘要:本章主要是学习的文件操作,主要是从文件中读取数据以及将数据存储到文件中,还有错误处理,异常类,模块等。函数返回一个文件对象,用于接收该对象。异常中使用被称为异常的特殊对象来管理程序执行期间发生的错误。 《Python编程:从入门到实践》笔记。本章主要是学习Python的文件操作,主要是从文件中读取数据以及将数据存储到文件中,还有错误处理,异常类,json模块等。 1. 从文件中读数据 ...
摘要:名片管理系统新建名片全部名片查询名片修改名片退出系统功能新建名片输入返回上一层请输入姓名姓名长度不符合位以内请输入姓名请输入年龄请输入电话号码电话号码长度不符合位请输入电话号码请输入号码请输入电子邮箱请输入所属公司电话号码长度不符合位请输 list1 = [] def show_card(): print(****************************************...
摘要:前言数据模型其实是对框架的描述,它规范了这门语言自身构件模块的接口,这些模块包括但不限于序列迭代器函数类和上下文管理器。上述类实现了方法,它可用于需要布尔值的上下文中等。但多亏了它是特殊方法,我们也可以把用于自定义数据类型。 《流畅的Python》笔记。本篇是Python进阶篇的开始。本篇主要是对Python特殊方法的概述。 1. 前言 数据模型其实是对Python框架的描述,它规范了...
摘要:基础之控制结构学习目标代码块与缩进条件语句语句语句的嵌套断言循环循环循环中断循环控制语句综合嵌套列表解析式基础相关链接学习目标是简洁易学面向对象的编程语言。 Py...
阅读 3816·2021-09-22 15:34
阅读 1342·2019-08-29 17:25
阅读 3507·2019-08-29 11:18
阅读 1511·2019-08-26 17:15
阅读 1882·2019-08-23 17:19
阅读 1358·2019-08-23 16:15
阅读 824·2019-08-23 16:02
阅读 1492·2019-08-23 15:19