input和while循环——Python编程从入门到实践
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 Python!
Hello Python!
1. 编写清晰的程序
name = input("Please enter your name: ")
print("Hello, " + name + "!")
Please enter your name: hery
Hello, hery!
提示信息超过一行时:
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += '\nWhat is your name? '
name = input(prompt)
print("\nHello, " + name + "!")
2. 获取数值的输入
age = input("How old are you? ")
print(type(age))
How old are you? 12
<class 'str'>
通过input()函数输入的信息以字符串的形式存储,若需要将输入作为数值使用怎么办呢?
可以使用int()函数将其转换为数值表示:
height = input("How tall are you, in inches? ")
height = int(height)
if height >= 36:
print("\nYou're tall enough to ride!")
else:
print("\nYou'll be able to ride when you're a little older.")
3. 求模运算符
求模运算符(%):求得两数相除返回的余数。
可用于判断一个数是奇数还是偶数:
number = input("Enter a number, and I'll tell you if it is even or odd: ")
number = int(number)
if number % 2 == 0:
print('\nThe number ' + str(number) + ' is even.')
else:
print('\nThe number ' + str(number) + ' is odd.')
运算符两端的元素类型要一致,故print语句中又需要将数值型通过str()函数转换为字符型。
while循环
for循环是针对集合中的每个元素的一个代码块,而while循环是不断的运行,直到指定条件不满足。
1. 使用while循环
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
运行结果:
1
2
3
4
5
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)
print(message)
运行结果:
Tell me something , and I will repeat it back to you:
Enter 'quit' to end the program. Hello Python
Hello Python Tell me something , and I will repeat it back to you:
Enter 'quit' to end the program. Hello 0629
Hello 0629 Tell me something , and I will repeat it back to you:
Enter 'quit' to end the program. quit
quit
输入为 quit 时循环结束。
若不想将 quit 也作为一条消息打印出来,则:
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)
3. 使用标志
在要求很多条件都满足的情况下才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态,这个变量称为标志。
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)
敲代码的时候把 active = False 敲成了 active = 'False',然后输入quit还一直执行循环,哈哈哈
4. 使用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() + "!")
Note: Python循环(while循环、for循环)中都可使用break语句来推出循环。
5. 在循环中使用continue
循环中使用continue,会返回大循环开头,并根据条件测试结果决定是否继续执行循环:
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
else:
print(current_number)
运行结果:
1
3
5
7
9
6. 避免无限循环
x = 1
while x < 5:
print(x)
x += 1
上述的代码块中,若漏写了代码行 x += 1,这个程序将没完没了地运行。可按Ctrl + C,也可关闭显示程序输出的终端窗口,或关闭编辑器,结束无限循环。
input和while循环——Python编程从入门到实践的更多相关文章
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
- 《python编程从入门到实践》读书实践笔记(一)
本文是<python编程从入门到实践>读书实践笔记1~10章的内容,主要包含安装.基础类型.函数.类.文件读写及异常的内容. 1 起步 1.1 搭建环境 1.1.1 Python 版本选择 ...
- Python编程从入门到实践笔记——异常和存储数据
Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...
- Python编程从入门到实践笔记——文件
Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...
- Python编程从入门到实践笔记——函数
Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...
- Python编程从入门到实践笔记——if语句
Python编程从入门到实践笔记——if语句 #coding=utf-8 cars=['bwm','audi','toyota','subaru','maserati'] bicycles = [&q ...
- Python编程从入门到实践笔记——操作列表
Python编程从入门到实践笔记——操作列表 #coding=utf-8 magicians = ['alice','david','carolina'] #遍历整个列表 for magician i ...
- 《Python编程从入门到实践》_第十章_文件和异常
读取整个文件 文件pi_digits.txt #文件pi_digits.txt 3.1415926535 8979323846 2643383279 下面的程序打开并读取整个文件,再将其内容显示到屏幕 ...
- #Python编程从入门到实践#第四章笔记
#Python编程从入门到实践#第四章笔记 操作列表 1.遍历列表 使用for循环,遍历values列表 for value in values: print(value) 2.数字列表 使 ...
随机推荐
- LeetCode 1215. Stepping Numbers
原题链接在这里:https://leetcode.com/problems/stepping-numbers/ 题目: A Stepping Number is an integer such tha ...
- 洛谷 P2746 [USACO5.3]校园网 Network of Schools 题解
Tarjan 模板题 第一问就是缩点之后看有多少个入度为零的点就好了. 第二问是在缩点后将每个点的入度和出度都求出(只要有入度或出度就置为1),然后比较哪个有值的多,将多的作为答案输出.原因是由题可得 ...
- python 字符串方法整理
Python字符串方法 1.大小写转换 1.1 lower.upper lower():小写 upper():大写 1.2 title.capitalize S.title():字符串中所有单词首字母 ...
- learing java NIO 之 ReadFile
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- 13-ESP8266 SDK开发基础入门篇--上位机串口控制 Wi-Fi输出PWM的占空比,IEEE754规约
https://www.cnblogs.com/yangfengwu/p/11100552.html 这节做个上位机控制Wi-Fi引脚输出的PWM占空比信号,灯的亮度就可以用上位机控制了 大家可以自己 ...
- THUPC&CTS 2019 游记
day ? 去THU报了个到. day? THUPC比赛日,三个人都没有智商,各种签到题不会做,被各路神仙吊着打.G题还猜了个假结论,做了好久都不对.最后顺利打铁了. 还顺便去看一下THUAC. da ...
- 括号匹配(POJ2955)题解
原题地址:http://poj.org/problem?id=2955 题目大意:给出一串括号,求其中的最大匹配数. 我这么一说题目大意估计很多人就蒙了,其实我看到最开始的时候也是很蒙的.这里就来解释 ...
- linux命令之------Cat命令
Cat命令 作用:cat命令用于连接文件并打印,查看文件内容: -n或--number:由1开始对所有输出的行数编号: -b或--number-nonblank:和-n相似,只不过对于空白行不做编号: ...
- 从宿主机直接进入docker容器的网络空间
Docker dns nameserver 也是进入容器网络空间,监听53端口,但它通过iptable把端口映射到宿主机上,处理DNS请求的进程就在宿主机上. how does Docker Embe ...
- [转]eclipse中 properties文件编码问题
原文地址:https://blog.csdn.net/uestcong/article/details/6635123 1. Eclipse修改设置项目中用到了配置文件,所以在Eclipse中新建.p ...