Python-if-elif-else语句

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

Source:

[python] view plain copy print ?
  1. #!/bin/env python  
  2. # coding=gb2312  
  3. # -*- coding: gb2312 -*-  
  4. from __future__ import division  
  5. #### if-else ####  
  6. print '#### if-else ####'  
  7. a = input("a: "# 12 or 10+2  
  8. b = input("b: ")  
  9. if(a>b):  
  10.     print "max: ", a  
  11. else:  
  12.     print "max: ", b  
  13. #### if-elif-else ####  
  14. print '#### if-elif-else ####'  
  15. score = raw_input("score: "# string  
  16. score = int(score)  
  17. if(score>=90and (score<=100):  
  18.     print "A"  
  19. elif(score>=80 and score<90):  
  20.     print "B"  
  21. elif(score>=60 and score<80):  
  22.     print "C"  
  23. else:  
  24.     print "D"  
  25. #### switch I ####  
  26. print '#### switch ####'  
  27. x = 1  
  28. y = 2  
  29. operator = "/"  
  30. result = {  
  31.     "+": x+y,  
  32.     "-": x-y,  
  33.     "*": x*y,  
  34.     "/": x/y  
  35. }  
  36. print result.get(operator)  
  37. #### switch II ####  
  38. print '#### switch II ####'  
  39. class switch(object):                 
  40.     def __init__(self, value):   # init value  
  41.         self.value = value  
  42.         self.fall = False        # no break, then fall=False      
  43.     def __iter__(self):  
  44.         yield self.match         # match method to create   
  45.         raise StopIteration      # exception to check loop  
  46.     def match(self, *args):  
  47.         if self.fall or not args:  
  48.             return True  
  49.         elif self.value in args: # successful  
  50.             self.fall = True  
  51.             return True  
  52.         else:                    # fail  
  53.             return False  
  54. operator = "+"  
  55. x = 1  
  56. y = 2  
  57. for case in switch(operator):  
  58.     if case('+'):  
  59.         print x+y  
  60.         break  
  61.     if case('-'):  
  62.         print x-y  
  63.         break  
  64.     if case('*'):  
  65.         print x*y  
  66.         break  
  67.     if case('/'):  
  68.         print x/y  
  69.         break  
  70.     if case():  
  71.         print 'NULL'  
#!/bin/env python# coding=gb2312# -*- coding: gb2312 -*-from __future__ import division#### if-else ####print '#### if-else ####'a = input("a: ") # 12 or 10+2b = input("b: ")if(a>b):    print "max: ", aelse:    print "max: ", b#### if-elif-else ####print '#### if-elif-else ####'score = raw_input("score: ") # stringscore = int(score)if(score>=90) and (score<=100):    print "A"elif(score>=80 and score<90):    print "B"elif(score>=60 and score<80):    print "C"else:    print "D"#### switch I ####print '#### switch ####'x = 1y = 2operator = "/"result = {    "+": x+y,    "-": x-y,    "*": x*y,    "/": x/y}print result.get(operator)#### switch II ####print '#### switch II ####'class switch(object):                   def __init__(self, value):   # init value        self.value = value        self.fall = False        # no break, then fall=False        def __iter__(self):        yield self.match         # match method to create         raise StopIteration      # exception to check loop    def match(self, *args):        if self.fall or not args:            return True        elif self.value in args: # successful            self.fall = True            return True        else:                    # fail            return Falseoperator = "+"x = 1y = 2for case in switch(operator):    if case('+'):        print x+y        break    if case('-'):        print x-y        break    if case('*'):        print x*y        break    if case('/'):        print x/y        break    if case():        print 'NULL' 

Result:

$ python if_else.py 

#### if-else ####

a: 12 + 8

扫描二维码关注公众号,回复: 3896970 查看本文章

b: 30

max:  30

#### if-elif-else ####

score: 88

B

#### switch ####

0.5

#### switch II ####

3

================================================================

中文注释参考:

 

一个极小的问题。在python代码中,用了中文注释,不能被python解释器理解(python 2.5)。解决方案是:

# coding=gb2312
print 'ok'   # 中文注释没问题

或者:

# -*- coding: gb2312 -*-
print 'ok'  #这样也行


if-else 多种用法

#!/usr/bin/python# -*- coding:utf8 -*- ## blog.ithomer.neta, b, c = 1, 2, 3# 常规def test1():    if a>b:        c = a    else:        c =b    print c# 表达式def test2():    c = a if a>b else b    print c# 二维列表def test3():    c = [b,a][a>b]    print c# 黑客用法def test4():    c = (a>b and [a] or [b])[0]    print cif __name__ == "__main__":    test1()    test2()    test3()    test4()
运行结果:
2
2
2
2

 


博客之星评选,请投我一票:

http://vote.blog.csdn.net/blogstaritem/blogstar2013/sunboy_2050


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/dghggij/article/details/83513849