5.Python之input和while
概述
接收用户输入是程序的一个重要功能,而while循环可以让程序不断运行,直到判断条件不满足为止。
通过获取用户输入并学会控制程序的运行时间,可编写除交互式程序。input()
##--------input() get string
message=input("Tell me something,and I will repeat it back to you: ")
print(message)
##--------input() get value
question="How old are you? "
age = input(question)
print(age)
可以将提示存储到变量中,再讲该变量传递给input()函数。
- while()
for循环用于处理集合中的每个元素,而while循环不断地运行,直到判断条件不满足为止。
current_number = 1
while current_number <= 5:print(current_number)current_number += 1prompt = "\nTell me something,and I will repeat it back to you: "
prompt += "\nEnter 'quit' to end the program.\n"
message = ""
while message != 'quit':#until to Not-meetmessage =input(prompt)print(message)
美中不足的是把quit也打印出来了,可以通过一个if判断修复这个问题。
prompt = "\nTell me something,and I will repeat it back to you: "
prompt += "\nEnter 'quit' to end the program.\n"
message = ""
while message != 'quit':#until to Not-meetmessage =input(prompt)if message !="quit":print(message)
- break VS continue
break:立刻退出循环,不再运行循环中余下的代码,不管条件判断结果如何都退出整个循环。
任何python循环都可以使用break语句。
continue:返回到循环开头,并根据条件判断结果决定是否运行。
##==break
prompt = "\nTell me something,and I will repeat it back to you: "
prompt += "\nEnter 'quit' to end the program.\n"
while True:city=input(prompt)if(city=='quit'):breakelse:print("I'd love to go to "+city.title()+" !")##==continue
current_number=0
while current_number<10:current_number+=1;if current_number%2==0:continueelse:print(current_number)
while语句需要有停止运行的条件(如果需要停止的话),务必对每个 while循环进行测试。
如果程序陷入无限循环,按Ctrl+C可以停止,关机也好使。
- 使用while()
##==use while to get infomation
responses={}#dict
polling_active=True
while polling_active:name=input("What's your name?\n")response=input("Which mountain would you like to climb someday?\n")responses[name]=responserepeat=input("Would you like to let another person respond?(yes/no)\n")if repeat=='no':polling_active=False
使用while进行调查,获取信息。
pets=['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)while 'cat' in pets:pets.remove('cat')
print(pets)
删除包含特定值的列表元素。while一直等到pets中没有cat才退出。
就酱!
5.Python之input和while相关推荐
- 简单介绍python的input,print,eval函数
这篇文章主要为大家概述了python的input,print,eval函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助 input()函数 input()函数可以通过控制台 ...
- python使用input函数时、必须添加提示文字-浅谈python3中input输入的使用
今天谈一下关于python中input的一些基本用法(写给新手入门之用,故只谈比较实用的部分). 首先,我们可以看一下官方文档给我们的解释(在python的shell中输入命令即可): 上面的英文说的 ...
- python输入一组数字存到列表_Python如何使用输入传递多个值并将它们存储在一个列表中(简单版本),python,怎么,input,传入,储存,到...
Python如何使用输入传递多个值并将它们存储在一个列表中(简单版本),python,怎么,input,传入,储存,到 发表时间:2020-08-20 当传入多个值时,需要使用split()函数来切割 ...
- python输出星号等腰三角形_星号三角形Python(带输入)(Asterisk Triangle Python (with input))...
星号三角形Python(带输入)(Asterisk Triangle Python (with input)) 我正在做一个关于python 3的初学者课程,并且必须形成一个星号三角形,输出如下. 星 ...
- python 使用input函数输入两个数字,比较两个数字的大小,并输出较大的数
# python 使用input函数输入两个数字,比较两个数字的大小,并输出较大的数 """ 任务 1.定义变量x和y,用于存放输入的两个数值 2.判断x与y是否相等,相 ...
- python中input()函数的返回是什么类型_Python中input函数的用法是什么?
"input()"函数是输入函数,用于接受一个标准输入数据,且返回string类型.在Python3中,去除了"raw_input()"函数,仅保留了" ...
- python中input的意思_python中input指的是什么意思
python中input指的是什么意思 发布时间:2020-11-20 14:05:44 来源:亿速云 阅读:85 作者:小新 这篇文章主要介绍python中input指的是什么意思,文中介绍的非常详 ...
- python使用input输入数组
python使用input输入数组 今天学习排序算法时教程给的都是固定数组,想了一下怎么使用input输入,最后代码如下 一维数组: # 输入一个一维数组 arr = input() # 将输入每个数 ...
- python的input函数_python利用input函数输入多个参数的方法
python利用input函数输入多个参数的方法 发布时间:2020-05-23 17:23:48 来源:亿速云 阅读:489 作者:鸽子 python进行交互式输入过程中,一般使用input()函数 ...
- Python限制input输入时间
ZACKZHOU 秋二 最近遇到一个python的需求,大概的意思就是限制input输入的时间. 我们先来了解一下python的input函数,它主要是接受一个标准输入数据,返回为 string 类型 ...
最新文章
- html完成横纵坐标位置确定,2018-2019学年安徽省合肥市七年级下期中数学试卷-附详细答案...
- oracle取32位唯一码,Oracle导出导入dmp文件命令,以及excel生成32位uuid
- 百度:请叫我卡车新势力
- Mysql之drop、truncate和delete的区别
- 【Powershell】【性能计数器】基础信息的获取
- 按 字节截取分别以GBK 和 utf-8 编码的 字符串的java程序。
- 微信宣布:被禁 8 年的限制解除了!
- Linux操作系统 (二)下载以及安装
- 土地利用转移矩阵--更新中
- SpringBoot整合Cache缓存技术(二十一)
- 大话西游服务器维护多长时间,《大话西游3》2010-9-21服务器停机维护公告(二)...
- Android仿微信通讯录
- sql server 替换字段中的部分字符,替换指定字符的方法
- 鼠标作为画笔 | 八
- apmserver导入MySQL_APMServ MySQL 错误
- 美国英文简写日期格式转换
- linux 下 PHP 环境搭建(已测试)
- 18.AtomicReference、AtomicStampReference底层原理。多个变量更新怎么保证原子性?CAS的ABA问题怎么解决?
- jk触发器上升沿怎么看_jk触发器波形图_jk触发器波形图怎么画
- 基于微信电子书阅读小程序毕业设计毕设作品(6)开题答辩PPT