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 =
input("How tall are you, in inches? ")

02 height =
int(height)

03 if height >=
36:

04
print("\nYou're tall enough to ride!")

05 else:

06
print("\nYou'll be able to ride when you're a little older.")

>>>

 
 

You'll be able to ride when you're a little older.

7.3 求模运算

目的:求模运算符 (%) 是一个很有用的工具, 它将两个数相除并返回余数

01 number =
input("Enter a number, and I'll tell you if it's even or odd: ")

02 number =
int(number)

03 if number %
2
==
0:

04
print("\nThe number "
+
str(number) +
" is even.")

05 else:

06
print("\nThe number "
+
str(number) +
" is odd.")

>>>

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
if message=='quit':

07 act=False;

08
else:

09
print(message);

>>>

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
if message=='quit':

08
break;

09
else:

10
print(message);

11

12

13 current_number =

14 while current_number <
10:

15 current_number +=

16
if current_number %
2
==
0:

17
continue

18
print(current_number)

>>>

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',
'cat',
'dog',
'goldfish',
'cat',
'rabbit',
'cat'];

05 while
'cat'
in pets:

06 pets.remove('cat');

07 print(pets);

08

09 # tip2:del

10 pets = ['dog',
'cat',
'dog',
'goldfish',
'cat',
'rabbit',
'cat'];

11 index=0;

12 while
'cat'
in pets:

13
if pets[index]=='cat':

14
del pets[index];

15 index=index;

16
else:

17 index+=1;

18 print(pets);

19

20 # tip1 is better than tip2

>>>

['dog', 'dog', 'goldfish', 'rabbit']

['dog', 'dog', 'goldfish', 'rabbit']

  • 使用用户输入填充

     
     

     
     

     
     

     
     

     
     

     
     

     
     

     
     

     
     

     
     

     
     

第七章 用户输入和while 循环的更多相关文章

  1. Python:从入门到实践--第七章--用户输入和while循环-练习

    #1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...

  2. 第七章 用户输入和while循环

    7.1函数input()的工作原理 函数默认输入为字符串string,如果需使用数字,需用int进行类型转换 7.2 while循环 while是根据条件的真假判断是否进入执行 使用标志: 使用bre ...

  3. 第七章 用户输入和while语句

    大多数编程都旨在解决最终用户的问题,为此通常需要从用户那里获取一些信息.例如,假设有人要判断自己是否到了投票的年龄,要编写回答这个问题的程序,就需要知道用户的年龄,这样才能给出答案.因此,这种程序需要 ...

  4. python从入门到实践-7章用户输入和while循环

    #!/user/bin/env python# -*- coding:utf-8 -*- # input() 可以让程序暂停工作# int(input('please input something: ...

  5. Python编程从入门到实践笔记——用户输入和while循环

    Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...

  6. python入门学习:6.用户输入和while循环

    python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...

  7. 《Python编程从入门到实践》_第七章_用户输入和whlie循环

    函数input()的工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,python将其存储在一个变量中,以方便你使用. #输入用户名 username = input( ...

  8. 用户输入与while循环

    函数input()的工作原理: 函数input()让程序短暂运行,等待用户输入一些文本,获取用户输入后将其存储在一个变量中 测试input()功能-- #!/usr/bin/env python#fi ...

  9. python的用户输入和while循环

    1.函数input()工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. (1)获取数值可以用 int()函数 (2)求 ...

随机推荐

  1. P3808 【模版】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本 ...

  2. React 16 服务端渲染的新特性

    React 16 服务端渲染的新特性 React 16 中关于服务端渲染的新特性 快速介绍React 16 服务端渲染的新特性,包括数组.性能.流等 React 16 终于来了!

  3. 揭秘IPHONE X刷脸认证的技术奥秘

    苹果最新发布的Iphone X具有一个全新的功能叫做刷脸认证,背后的技术其实是生物密码的更新,通过人脸识别取代了传统的指纹识别,大家肯定对这种新技术非常感兴趣,下面我们通过这篇文章为大家介绍人脸识别的 ...

  4. size_t与size_type的使用

    size_t 是为了方便系统之间的移植而定义的 在32位系统上 定义为 unsigned int在64位系统上 定义为 unsigned long 更准确地说法是 在 32位系统上是32位无符号整形在 ...

  5. Python学习笔记基础篇-(1)Python周边

    一.系统命令 1.Ctrl+D 退出Python IDLE input方法中输入EOF字符,键入Ctrl+D 2.命令行选项: -d   提供调试输出 -O 生成优化的字节码(.pyo文件) -S 不 ...

  6. ML:自然语言处理NLP面试题

    自然语言处理的三个里程碑: http://blog.csdn.net/sddamoke/article/details/1419973 两个事实分别为: 一.短语结构语法不能有效地描写自然语言. 二. ...

  7. salt-master迁移

    1.在迁移到的目标机器上先安装salt-master 2.把原master机器上的/etc/salt/pki目录打包发送到迁移的机器上的同等目录下面 3.在原master机器上批量修改minion的配 ...

  8. 编写模块时的声明(含MODULE_LICENSE等)(转)

    编写模块必须先声明下面两句: #include <linux/module.h>               //这个头文件包含了许多符号与函数的定义,这些符号与函数多与加载模块有关 #i ...

  9. iframe里面开启全屏allowfullscreen="true"

    <iframe id="J_iframe" width="100%" height="100%" src="client-a ...

  10. 洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 性质分析

    Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...