第七章 用户输入和while 循环
7.1 创建多行字符串的方式:
|
01 prompt="if you tell me who you are, we can personalize the message you see." 02 prompt+="what is your first name? "; 03 04 name=input(prompt); 05 print("\nhello,"+name+'!'); >>> if you tell me who you are, we can personalize the message you see.what is your first name? franz hello,franz! |
7.2 使用函数int()获得整数输入
|
01 height = 02 height = 03 if height >= 04 05 else: 06 >>> You'll be able to ride when you're a little older. |
7.3 求模运算
目的:求模运算符 (%) 是一个很有用的工具, 它将两个数相除并返回余数
|
01 number = 02 number = 03 if number % 04 05 else: 06 >>> Enter a number, and I'll tell you if it's even or odd: 45 The number 45 is odd. |
7.4 while 循环:
- 目的:for 循环用于针对集合中的每个元素都一个代码块, 而while 循环不断地运行, 直到指定的条件不满足为止
- 使用标志判定程序的活动状态:
|
01 prompt="\nTell me sth,and I will repeat back to you"; 02 prompt+="\nEnter 'quit' to end the program. "; 03 act=True; 04 while act: 05 message=input(prompt); 06 07 act=False; 08 09 >>> Tell me sth,and I will repeat back to you Enter 'quit' to end the program. no no Tell me sth,and I will repeat back to you Enter 'quit' to end the program. yes yes Tell me sth,and I will repeat back to you Enter 'quit' to end the program. quit |
- break语句与continue语句:
|
01 # Break test & continue tst 02 03 prompt="\nTell me sth,and I will repeat back to you"; 04 prompt+="\nEnter 'quit' to end the program. "; 05 while True: 06 message=input(prompt); 07 08 09 10 11 12 13 current_number = 14 while current_number < 15 current_number += 16 17 18 >>> Tell me sth,and I will repeat back to you Enter 'quit' to end the program. quit 1 3 5 7 |
- 删除包含特定值的所有列表元素(方法remove()与关键词del)
|
01 # test:remove specific element 02 03 # tip1:remove() 04 pets = ['dog', 05 while 06 pets.remove('cat'); 07 print(pets); 08 09 # tip2:del 10 pets = ['dog', 11 index=0; 12 while 13 14 15 index=index; 16 17 index+=1; 18 print(pets); 19 20 # tip1 is better than tip2 >>> ['dog', 'dog', 'goldfish', 'rabbit'] ['dog', 'dog', 'goldfish', 'rabbit'] |
- 使用用户输入填充
第七章 用户输入和while 循环的更多相关文章
- Python:从入门到实践--第七章--用户输入和while循环-练习
#1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...
- 第七章 用户输入和while循环
7.1函数input()的工作原理 函数默认输入为字符串string,如果需使用数字,需用int进行类型转换 7.2 while循环 while是根据条件的真假判断是否进入执行 使用标志: 使用bre ...
- 第七章 用户输入和while语句
大多数编程都旨在解决最终用户的问题,为此通常需要从用户那里获取一些信息.例如,假设有人要判断自己是否到了投票的年龄,要编写回答这个问题的程序,就需要知道用户的年龄,这样才能给出答案.因此,这种程序需要 ...
- python从入门到实践-7章用户输入和while循环
#!/user/bin/env python# -*- coding:utf-8 -*- # input() 可以让程序暂停工作# int(input('please input something: ...
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
- python入门学习:6.用户输入和while循环
python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...
- 《Python编程从入门到实践》_第七章_用户输入和whlie循环
函数input()的工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,python将其存储在一个变量中,以方便你使用. #输入用户名 username = input( ...
- 用户输入与while循环
函数input()的工作原理: 函数input()让程序短暂运行,等待用户输入一些文本,获取用户输入后将其存储在一个变量中 测试input()功能-- #!/usr/bin/env python#fi ...
- python的用户输入和while循环
1.函数input()工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. (1)获取数值可以用 int()函数 (2)求 ...
随机推荐
- 5个对话框和FileStream:文件流
1.private void button1_Click(object sender, EventArgs e) { colorDialog1.ShowDialog();//显示颜色选择器 panel ...
- ES6变量的解构赋值
变量的解构赋值 1.数组的解构赋值 2.对象的解构赋值 3.字符串的解构赋值 4.数值和布尔值的解构赋值 5.函数参数的解构赋值 6.圆括号问题 7.用途 1.数组的解构赋值 ES6 允许写成下面这样 ...
- hihoCoder挑战赛31
#1595 : Numbers 时间限制:8000ms 单点时限:1000ms 内存限制:256MB 描述 给定n个整数常数c[1], c[2], ..., c[n]和一个整数k.现在需要给2k个整数 ...
- MacType 缺字问题【转】
- 【Oracle】查询当前SCN
介绍两种方式: 一.sys用户下: select current_scn from v$database; select dbms_flashback.get_system_change_number ...
- C#中的引用传递和值传递。
最近在写项目中有同事碰到这样的传值问题,可能很多对这个参数的传递还有点疑惑,自己也是对这个基础知识做一个回顾和巩固. 首先什么是值类型和引用类型可以去园里看一下相关的资料,都有介绍. 常用值类型包括: ...
- 继承&封装
扩展一个已有的类,并且继承该类的属性和行为这种方式成为继承. 实例 public class Polygon { public int sides; public double area; publi ...
- ubuntu16.04下编译安装OpenCV
一: 预先配置 为使OpenCV的安装在编译时更完备,预先安装好所有的开发平台: 二:编译OpenCV 在OpenCV官网下载UNIX的源码包: 安装一下软件: sudo apt-get instal ...
- 图像检索中为什么仍用BOW和LSH
原文链接:http://blog.csdn.net/jwh_bupt/article/details/27713453 去年年底的时候在一篇博客中,用ANN的框架解释了BOW模型[1],并与LSH[2 ...
- RxSwift學習教程之基礎篇
前言 我們在 iOS 開發過程中,幾乎無時無刻都要面對異步事件的處理.例如,按鍵點擊.數據保存..音頻後臺播放.交互動畫展示.這些事件並不具備特定時序性,甚至它們可能同時發生. 雖然 Apple 提供 ...