if elif else

今天学习了if语句,包括if - else, if -elif - else, 以及测试多个条件

学习代码如下,参考书籍主要为《pyhton 从入门到实践》

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Wed Feb 20 18:42:20 2019
 4 
 5 @author: ZCL
 6 """
 7 
 8 #学习 if 语句
 9 cars=['bmw','Aodi','dazhong','toyota']
10 for car in cars:
11     car=car.lower()
12     if car=='aodi':
13         print car.title()
14     elif car=='bmw':
15         print car.title()
16 number=[1,2,3,4,5]
17 for i in number:
18     if i==3:
19         print ("find 3!"),"",
20     else:
21         print("No 3!"),"",
22 #使用and测试多个条件
23 print "\n"        
24 j=int(raw_input())
25 while j>=10 and j<=100:
26     print "liangweishu"
27     j=int(raw_input())
28 print "bushiliangweishu"
29 # if-elif-else 语句
30 age = 12
31 if age < 4:
32     print("Your admission cost is $0.")
33 elif age < 18:
34     print("Your admission cost is $5.")
35 else:
36     print("Your admission cost is $10.")
37 #更简洁
38 age=12
39 if age<4:
40     price=0
41 elif age<18:
42     price=5
43 else:
44     price=10
45 print "Your admission cost is $"+str(price)
46 #测试多个条件(使用lower获取小写)
47 #使用了多个if-else语句
48 cars=[]
49 Cars=['bmw','Aodi','dazhong','toyota']
50 for car in Cars:
51    car=car.lower()
52    cars.append(car)
53 print cars
54 if 'bmw' in cars:
55     print "Have bmw"
56 if 'aodi' in cars:
57     print "Have aodi" 
58 else:
59     print "Have not aodi"  
60 if 'dongfeng' in cars:
61     print "Have dongfeng" 
62 else:
63     print "Have not dongfeng"
64 
65         

执行结果:

  

并且重新复习了遍历列表 ,以及向空列表里添加元素

猜你喜欢

转载自www.cnblogs.com/zcl1997/p/10415130.html