zoukankan      html  css  js  c++  java
  • 用户输入与while循环

    函数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 = input("how old are you? ")
    how old are you?
    26
    >>> 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 = " Tell me something and, I will repeat back to you: "

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

    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 = " Tell me something and, I will repeat back to you: "

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

    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

  • 相关阅读:
    ftp
    vmware虚拟机如何安装ubuntu14.10系统
    第1章 初识java----Java简介
    fiddler
    Program Files 与Program Files (x86)
    跟我一起认识axure(三)
    React-FlipOver-Counter(日历翻页)
    vue2-vux-fitness-project
    cloud-music
    跟我一起认识axure(二)
  • 原文地址:https://www.cnblogs.com/alben-cisco/p/6822981.html
Copyright © 2011-2022 走看看