zoukankan      html  css  js  c++  java
  • python 条件判断

    计算机之所以能够很多自动化的任务,因为它可以自己做条件判断:
    
    比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现:
    
    
    age = input('please input your age!')
    print age;
    if age >= 18:
        print 'your age is', age
        print 'adult'
    else:
        print  'your age is', age
        print 'man'
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    please input your age!10
    10
    your age is 10
    man
    
    Process finished with exit code 0
    
    
    
    
    age = raw_input('please input your age!')
    print age;
    if age >= 18:
        print 'your age is', age
        print 'adult'
    else:
        print  'your age is', age
        print 'man'
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    please input your age!10
    10
    your age is 10
    adult
    
    Process finished with exit code 0
    
    此时用raw_input就不行
    
    if语句执行有个特点,它是从上往下判断,如果在某个判断上是True,
    
    把该判断对应的语句执行后,就忽略掉剩下的elif和else,所以,
    
    请测试并解释为什么下面的程序打印的是teenager:
    
    
    
    
    age = 20
    if age >= 6:
        print 'teenager'
    elif age >= 18:
        print 'adult'
    else:
        print 'kid'
    
    
    循环:
    
    Pytho的循环有两种,一种是for..in,因此把list或tuple中的每个元素迭代出来,看例子:
    
    
    names = ['Michael', 'Bob', 'Tracy']
    for name in names:
        print name
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/t1.py
    Michael
    Bob
    Tracy
    
    所以for x in ... 循环就是把每个元素带入变量x,然后执行缩进快的语句:
    
    sum = 0
    for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
        sum = sum + x
    print sum
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/t1.py
    55
    
    如果要计算1-100的整数之和,从1写到100有点困难,幸好Python提供了一个range()函数,可以生成
    
    一个整数序列,比如range(5)生成的序列是从0开始小于5的整数:
    
    a=range(5)
    print a
    print a[0];
    print a[1];
    print a[2];
    print a[3];
    print a[4];
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/t1.py
    [0, 1, 2, 3, 4]
    0
    1
    2
    3
    4
    
    第二种循环是while循环,只要条件满足,就不断循环,条件不满足就退出循环。
    
    sum = 0
    n = 99
    while n > 0:
        sum = sum + n
        n = n - 2
    print sum
    
    
    再议raw_input:
    
    最后看一个有问题的条件判断,使用raw_input()读取用户的输入,这样可以自己输入,程序运行得更有意思:
    
    输入1982,结果却显示00后,这么简单的判断Python也能搞错?
    
    当然不是Python的问题,在Python的交互式命令行下打印birth看看:
    
    >>> birth
    '1982'
    >>> '1982' < 2000
    False
    >>> 1982 < 2000
    True
    
    原因找到了!原来raw_input()读取的内容永远以字符串的形式返回,
    
    把字符串和整数比较就得不到期待的结果.
    
    
    # -*- coding: UTF-8 -*-
    import time
    i=0;
    while 1 == 1:
        i=i+1;
        time.sleep(5);
        print i
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/t1.py
    1
    2
    3
    4
    

  • 相关阅读:
    定时任务:crontab: installing new crontab
    报错:Sqoop Failing this attempt. Failing the application.
    数据库优化
    vscode ,保存失败提示:关于Failed to save 'package.json': The content of the file is newer. Please..解决办法
    php -1 month 的问题
    sql 中convert和cast区别
    [SQL case when的两种用法]
    Android编译Lame库(Mp3编解码库)
    AndroidStudio使用Cmake编译armeabi-v7a,arm64-v8a的so库
    图片压缩原理
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349626.html
Copyright © 2011-2022 走看看