python学习之路之六--input与while
来源:互联网 发布:让人喜欢的女生知乎 编辑:程序博客网 时间:2024/06/15 06:44
python学习之input与while:
1.函数input()
函数input()会暂停程序,等待用户输入文本,同时它接受一个参数,起到提示和说明的作用
message = input("Tell me something,and I will repeat it back to you!")print(message)
2.使用input()获取的值
使用函数input()获取的值为str 类型 , 当你在输入int型需要比较时,则需要先将获取的值转换成int型在进行计算。
def addNum(a,b,c=5):sum = a + b + c;print(str(sum))while True:a = input("please input first num:")b = input("please input second num:")isInputThird = input('Are you input Third Num : y/n')if isInputThird == 'y':c = input('please input Third Num:')else :c = 0addNum(int(a),int(b),int(c))isContinue = input('Are you continue? y/n')if isContinue == 'y':continueelse:break运行结果:
3.求模运算符
求模运算符(%)将两个数相除并返回余数:
>>>4%31>>>5%32>>>6%30
4.while循环简介
for循环用于针对集合中的每个元素的一个代码块,而while循环不断地运行,直到指定的条件不满足为止
current_num = 1while current_num <= 5: print(current_num) current_num +=1
5.使用break退出循环
要立即退出循环 不在循环while下余下的代码,可使用break
6.使用continue 跳过没写操作
在循环中根据条件决定是否继续执行循环 ,他不会退出整个循环,而只是满足条件的情况下跳过本次循环,执行下次循环。
7.使用input来填充字典
responses = {}polling_active = Truewhile polling_active: name = input("\n what is you name?") response = input("which mountain would you like to climb someday?") responses[name] = response repeat = input("would you like to let another person respond?(yes/no)") if repeat == "no": polling_active = Falseprint("\n----Poll Result ----")for name,response in responses.items(): print(name+"would like to climb "+response +".")
0 0
- python学习之路之六--input与while
- python学习之while语句
- Python学习之While循环
- Python入门学习之While
- Python之input()与raw_input()
- Python学习之路六---迭代器、生成器
- Python学习六之文件
- Python学习笔记之while循环
- python学习(六)之函数与返回值
- 学习python之路---python小算法总结(六)
- Python之While循环
- Python之raw_input() 与 input() 函数
- python学习笔记之(六)
- 六、PYTHON 学习之装饰器使用
- python 数据结构六 之 字典与集合
- 【Java学习之路】for、while、do-while的使用
- python学习之语句:循环语句(for,while)
- Python之if判断语句与while和for循环
- 栈的压入、弹出序列
- windows注册表文件关联机制
- Vue---路由跳转和嵌套
- Cesium-3D Tiles 跳跃式层级细节
- [bigdata-069]mysql数据迁移从自建导入到rds+代码过滤增量导入 centos6.8+pymsql+mysqldump+source
- python学习之路之六--input与while
- jQuery基础篇(一)样式篇
- 自定义标签库
- 2.结构型模式
- Linux下打包压缩war和解压war包
- Eclipse使用git命令行
- 织梦自定义表单验证字段不能为空,绝对实用灵活
- python实现获取天气信息后自动登录邮箱发送到指定邮件联系人
- 【SqlServer】 分享 几个 优化 Sql 的 语句