函数input()的工作原理:
 函数input()让程序短暂运行,等待用户输入一些文本,获取用户输入后将其存储在一个变量中

测试input()功能——

#!/usr/bin/env python
#filename:input().py

message=input("tell me something and, I will repeat back to you: ")
print(message)

效果:

[root@Python-Test Day3]# ./input.py
tell me something and, I will repeat back to you: hello
hello

在有些时候,input()也许不能满足需求,例如:

>>> age=input("How old are you?")
How old are you?26
>>> age >= 18
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() >= int()

#可以发现这里出错了。因为input()函数存放用户输入的结果为字符串,字符串是无法和数字去比较的,

如何满足这类需求?#

通过int()函数,把字符串中的数字转换为整数


>>> age=int(age)
>>> age>=18
True

while循环

for循环用于针对集合中的每个元素的一个代码块,而while循环则是不断的运行,直到指定的条件不满足为止。

故while循环必须要设置一个打破条件,不然会无线循环下去!

用while来数数

#!/usr/bin/env python

#filename = num.py

number = 0

while number < 1000000000000000:

    print(number)

    number+=1

在这个循环中,设定了一个变量 number = 0

循环条件是number ≤ 100000000000000000,故这个程序一定会导致一段时间的刷频。

让用户选择何时退出:

#!/usr/bin/env python

#filename parrot.py

prompt = "\nTell me something and, I will repeat back to you:\n"

prompt += "Enter 'quit' to end the program\n"

message = ""

while message != 'quit':

    message=input(prompt)

    print(message)

#这里我们定义了一条提示信息,告诉用户他有两个选择

1、输入什么返回什么

2、输入quit结束程序

效果:#

[root@Python-Test Day3]# ./parrot.py 

Tell me something and, I will repeat back to you:
Enter 'quit' to end the program
hello
hello

Tell me something and, I will repeat back to you:
Enter 'quit' to end the program
cat
cat

Tell me something and, I will repeat back to you:
Enter 'quit' to end the program
quit
quit

[root@Python-Test Day3]# 

这个脚本的不足之处在于每次退出的时候,‘quit’也被print了

解决办法只需要加入一个if语句就可以了

while message != 'quit':

    message=input(prompt)

    if message != 'quit':

        print(message)

#加入一个判断,只有当message不等于‘quit’的时候,才会进行print

测试效果:

[root@Python-Test Day3]# ./parrot.py

Tell me something and, I will repeat back to you:

Enter 'quit' to end the program

hello

hello

Tell me something and, I will repeat back to you:

Enter 'quit' to end the program

quit

[root@Python-Test Day3]#

使用标志:

在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态,这个变量称之为标志。

#标志#可以看做程序的信号灯,可以让设置信号灯为true的时候程序继续运行,一旦信号灯=false,则程序立即终止。

#!/usr/bin/env python

#filename active.py

prompt = "\nTell me something and, I will repeat back to you:\n"

prompt += "Enter 'quit' to end the program\n"

active = True

while active:

    message=input(prompt)

    if message == 'quit':

        active = False

    else:

        print(message)

测试:

编写一个脚本,让程序可以列出1-30中的奇数

#filename even.py

number = 0

while number <= 29:

    number += 1

    if number % 2 == 0:

        continue

    print(number)     

首先,把number 设置成0,由于它小于29就开始进入了循环,每次+1,然后进行mod2运算(取余数),如果余数=0,则继续加1(因为偶数的余数都是0)

如果余数≠0,则进行print

用户输入与while循环的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. 读书笔记「Python编程:从入门到实践」_7.用户输入和while循环

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

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

    7.1 创建多行字符串的方式: 01 prompt="if you tell me who you are, we can personalize the message you see.& ...

  9. 用户输入和while 循环

    input 工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中. message = input("need to input ...

随机推荐

  1. require.js学习笔记

    使用require.js的好处? 1 有效的防止命名冲突(可以将变量封装在模块内,通过暴露出的接口解决命名冲突) 2 解决不同JS文件中的依赖 3 可以让我们的代码以模块化的方式组织 官方网站http ...

  2. 用MPLAB IDE编程时,软件总是弹出一个窗口提示: “the extended cpu mode configuration bit is enabled,but the program that was loaded was not built using extended cpu instructions. therefore,your code may not work properly

    用MPLAB IDE编程时,软件总是弹出一个窗口提示:"the extended cpu mode configuration bit is enabled,but the program ...

  3. Robotframe work之环境搭建(一)

    准备安装如下:Python2.7.10.robot framework3.0.2.wxPython 2.8.12.1.robot framework-ride 1. 官网下载安装python,目前wx ...

  4. Html 经典布局(三)

    经典布局案例(三): <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  5. Unity 学习Json篇

    介绍 JSON是一个简单的,但功能强大的序列化数据格式.它定义了简单的类型,如布尔,数(int和float)和字符串,和几个数据结构:list和dictionnary.可以在http://JSON.o ...

  6. nginx源码编译问题

    [root@localhost nginx-1.7.4]# ./configure checking for OS + Linux 2.6.32-431.el6.x86_64 x86_64 check ...

  7. 软件工程工具学习(1)---Visio

    要给15级软件工程上机了.开个系列记录软件工程开发过程中所会用到的一些工具的学习. 第一篇---软件分析与设计工具 Microsoft Visio Visio 介绍 1.Visio是一款矢量图形与图标 ...

  8. CF Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树变种

    题目链接:http://codeforces.com/problemset/problem/609/E 大致就是有一棵树,对于每一条边,询问包含这条边,最小的一个生成树的权值. 做法就是先求一次最小生 ...

  9. centos7 下nfs的配置

    td p { margin-bottom: 0cm } p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 补充知识: RPC 主程序: ...

  10. 谈谈web上种图片应用的优缺点

    web中承载信息的主要方式就是图片与文字了,以下就是对一些web图片格式的优缺点进行归纳. 1.GIF GIF图是比较古老的web图片格式之一,可以追溯到1987,几乎所有的浏览器都支持这一种格式,老 ...