漫天飞雪

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
内容:
流程控制if
  1 #语法1:
  2 # if 条件:
  3 #     code1
  4 #     code2
  5 #     code3
  6 #     ....
  7 
  8 # age=180
  9 # height=163
 10 # weight=75
 11 # sex='female'
 12 # is_beautiful=True
 13 #
 14 # if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
 15 #     print('表白。。。')
 16 #
 17 # print('=====>other code')
 18 
 19 
 20 #语法2:
 21 # if 条件:
 22 #     code1
 23 #     code2
 24 #     code3
 25 #     ....
 26 # else:
 27 #     code1
 28 #     code2
 29 #     code3
 30 #     ....
 31 
 32 # age=180
 33 # height=163
 34 # weight=75
 35 # sex='female'
 36 # is_beautiful=True
 37 #
 38 # if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
 39 #     print('表白。。。')
 40 # else:
 41 #     print('阿姨好')
 42 
 43 
 44 #语法3:多分枝
 45 # 强调:if的多分枝=但凡有一个条件成立,就不会再往下判断其他条件了
 46 # if 条件1:
 47 #     code1
 48 #     code2
 49 #     code3
 50 #     ....
 51 # elif 条件2:
 52 #     code1
 53 #     code2
 54 #     code3
 55 #     ....
 56 # elif 条件3:
 57 #     code1
 58 #     code2
 59 #     code3
 60 #     ....
 61 # ........
 62 # else:
 63 #     code1
 64 #     code2
 65 #     code3
 66 #     ....
 67 
 68 
 69 # 练习
 70 # 如果:成绩>=90,那么:优秀
 71 # 如果成绩>=80且<90,那么:良好
 72 # 如果成绩>=70且<80,那么:普通
 73 # 其他情况:很差
 74 # score = input('>>: ')
 75 # score=int(score)
 76 # if score >= 90:
 77 #     print('优秀')
 78 # elif score >= 80:
 79 #     print('良好')
 80 # elif score >= 70:
 81 #     print('普通')
 82 # else:
 83 #     print('很差')
 84 
 85 
 86 #语法4:if嵌套
 87 # age=18
 88 # height=163
 89 # weight=75
 90 # sex='female'
 91 # is_beautiful=True 当变量被赋给Ture,并用于条件时,代表如果出现此变量,是为真,恒成立。
 92 #
 93 # is_successfull=True
 94 #
 95 # if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
 96 #     print('表白。。。')
 97 #     if is_successfull:
 98 #         print('在一起')
 99 #     else:
100 #         print('我逗你玩呢')
101 # else:
102 #     print('阿姨好')
103 #
104 #
105 # print('other code....')
106 
107 
108 # 如果:今天是Monday,那么:上班
109 # 如果:今天是Tuesday,那么:上班
110 # 如果:今天是Wednesday,那么:上班
111 # 如果:今天是Thursday,那么:上班
112 # 如果:今天是Friday,那么:上班
113 # 如果:今天是Saturday,那么:出去浪
114 # 如果:今天是Sunday,那么:出去浪
115 
116 # today=input('>>: ')
117 # if today == 'Monday':
118 #     print('上班')
119 # elif today == 'Tuesday':
120 #     print('上班')
121 # elif today == 'Wednesday':
122 #     print('上班')
123 # elif today == 'Thursday':
124 #     print('上班')
125 # elif today == 'Friday':
126 #     print('上班')
127 # elif today == 'Saturday':
128 #     print('出去浪')
129 # elif today == 'Sunday':
130 #     print('出去浪')
131 # else:
132 #     print('''必须输入其中一种:
133 #     Monday
134 #     Tuesday
135 #     Wednesday
136 #     Thursday
137 #     Friday
138 #     Saturday
139 #     Sunday
140 #     ''')
141 today=input('>>: ')
142 
143 #多项条件执行的结果一致的话,可以使用or进行条件判断,只要满足其中一项,就能输出结果,达到了简化代码的效果。
144 if today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' or today == 'Thursday' or today == 'Friday':
145     print('上班')
146 elif today == 'Saturday' or today == 'Sunday':
147     print('出去浪')
148 else:
149     print('''必须输入其中一种:
150     Monday
151     Tuesday
152     Wednesday
153     Thursday
154     Friday
155     Saturday
156     Sunday
157     ''')
if条件控制
    流程控制while
  1 1.什么事循环 
  2 循环就是一个重复的过程
  3 
  4 2 为何要有循环
  5     人可以重复的去做某一件事
  6     程序中必须有一种机制能够控制计算机像人一样重复地去做某一件事
  7 
  8 
  9 3 如何用循环
 10 
 11 
 12 '''
 13 # 语法
 14 # while 条件:
 15 #     code1
 16 #     code2
 17 #     code3
 18 #     ...
 19 
 20 
 21 # user_from_db='egon'
 22 # pwd_from_db='123'
 23 #
 24 # while True:
 25 #     inp_user=input('please input your username: ')
 26 #     inp_pwd=input('please input your password: ')
 27 #     if inp_user == user_from_db and inp_pwd == pwd_from_db:
 28 #         print('login successfull')
 29 #     else:
 30 #         print('user or password err')
 31 
 32 # while + break: break代表结束本层循环
 33 # user_from_db='egon'
 34 # pwd_from_db='123'
 35 # while True:#此时有的人会觉得在这使用while循环,为什么,为什么加在这里,其实换个思维思考下,如果你上班一直坐的是111路公交车,
 36 这个条件是既定的吧,但是如果你坐过站了这个就是while下面需要执行的命令。加在这里是了下面的while循环可以按照同等条件一直执行,终止了while。那就是不再执行。
 37 #     inp_user=input('please input your username: ')
 38 #     inp_pwd=input('please input your password: ')
 39 #     if inp_user == user_from_db and inp_pwd == pwd_from_db:
 40 #         print('login successfull')
 41 #         break
 42 #     else:
 43 #         print('user or password err')
 44 
 45 
 46 # while+continue:continue代表结束本次循环(本次循环continue之后的代码不在运行),直接进入下一次循环
 47 # 强调:continue一定不要作为循环体的最后一步代码
 48 # start=1
 49 # while start < 8:   #5 < 8
 50 #     if start == 4:
 51 #         start += 1 #start=5
 52 #         continue   #当star等于4时,满足if的条件,本次的循环结束,请看清楚是“本次循环”,因为完整的循环还没有结束,当循环取值小于8时,循环体才结束,称之为本层循环结束
 53 #     print(start)
 54 #     start+=1
 55 注意:本次循环和本层循环所代表的意思
 56 
 57 # while True:
 58 #     print(1)
 59 #     print(2)
 60 #     print(3)
 61 # user_from_db='egon'
 62 # pwd_from_db='123'
 63 #
 64 # while True:
 65 #     inp_user=input('please input your username: ')
 66 #     inp_pwd=input('please input your password: ')
 67 #     if inp_user == user_from_db and inp_pwd == pwd_from_db:
 68 #         print('login successfull')
 69 #         break
 70 #     else:
 71 #         print('user or password err')
 72 #         continue
 73 
 74 
 75 #while循环的嵌套
 76 # user_from_db='egon'
 77 # pwd_from_db='123'
 78 #
 79 # while True:
 80 #     inp_user=input('please input your username: ')
 81 #     inp_pwd=input('please input your password: ')
 82 #     if inp_user == user_from_db and inp_pwd == pwd_from_db:
 83 #         print('login successfull')
 84 #         while True:
 85 #             cmd=input('>>>: ') # cmd='quit'
 86 #             if cmd == 'quit':
 87 #                 break
 88 #             print('%s run......' %cmd)
 89 #         break
 90 #     else:
 91 #         print('user or password err')
 92 
 93 
 94 # user_from_db='egon'
 95 # pwd_from_db='123'
 96 #
 97 # tag=True
 98 # while tag:
 99 #     inp_user=input('please input your username: ')
100 #     inp_pwd=input('please input your password: ')
101 #     if inp_user == user_from_db and inp_pwd == pwd_from_db:
102 #         print('login successfull')
103 #         while tag:
104 #             cmd=input('>>>: ') # cmd='quit'
105 #             if cmd == 'quit':
106 #                 tag=False
107 #                 break
108 #             print('%s run......' %cmd)
109 #     else:
110 #         print('user or password err')
111 #
112 
113 
114 # while True:
115 #     1+1
116 
117 
118 # while + else
119 # else的代码会在while循环没有break打断的情况下最后运行
120 # n=1
121 # while n < 5:
122 #     if n == 4:
123 #         break
124 #     print(n)
125 #     n+=1
126 # else:
127 #     print('=====》')
128 #
129 
130 
131 
132 
133 # user_from_db='egon'
134 # pwd_from_db='123'
135 #
136 # count=0
137 # tag=True
138 # while tag:
139 #     if count == 3:
140 #         print('输错次数过多')
141 #         break
142 #     inp_user=input('please input your username: ')
143 #     inp_pwd=input('please input your password: ')
144        #下面是代表如果计数器没达到3次,就可以进行执行下面输入用户名和密码的指令
145      if如果输入的是用户和密码是对的,有两个操作:
146                     1.告诉用户登录成功
147                     2.成功后用户可以继续执行接下来执行下面的操作,用户操作分为两种,
148                             1)是继续执行。  
149                             2)不执行,就使用if的条件语句进行判断,对输入的命令进行“==”验证。
150 
151 #     if inp_user == user_from_db and inp_pwd == pwd_from_db:
152 #         print('login successfull')
153 #         while tag:
154 #             cmd=input('>>>: ') # cmd='quit'
155 #             if cmd == 'quit':
156 #                 tag=False
157 #                 break
158 #             print('%s run......' %cmd)
159      输入错误,就会继续进行执行while循环,但是也不能一直循环下去吧,这里就需要进行条件的限制。如果输的是错误的,只能有上次机会,计数器输错一次就加一次,
160 如果输错三次,while的循环就会终止,让while True:编程while False:,False就表示是错误,那这样while循环体下面的coding命令就无法执行。
161 #     else:
162 #         print('user or password err')
163 #         count+=1 #count=3 # 输错3次
while循环
    流程控制for
 1 # names=['egon','alex_dsb','lxx_sb','yxx_dsb']
 2 # i=0
 3 # while i < len(names): #4 < 4
 4 #     print(names[i])
 5 #     i+=1
 6 
 7 
 8 # for循环:可以不依赖索引而取指
 9 # names=['egon','alex_dsb','lxx_sb','yxx_dsb']
10 # for item in names: #item='lxx_sb'
11 #     print(item)
12 
13 # dic={'x':1,'y':2,'z':3}
14 # for k in dic: #k='x'
15 #     print(k,dic[k])
16 
17 # for vs while
18 # for可以不依赖于索引取指,是一种通用的循环取指方式
19 # for的循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的
20 
21 
22 # names=['egon','alex_dsb','lxx_sb','yxx_dsb']
23 # for i in range(0,len(names)):
24 #     print(names[i])
25 
26 # for+break
27 # for+continue
28 
29 # for+else
30 # for i in range(10):
31 #     # if i == 4:
32 #     #     break
33 #     print(i)
34 # else:
35 #     print('====>')
for循环

 

小结:
有序无序+可变不可变
#1、有序:但凡有索引的数据都是有序的
#2、可变不可变
#可变类型:在值变了的情况下,id不变,证明在改原值
#不可变类型:在值变了的情况下,id也跟着变,证明不是在改原值

 



posted on 2018-07-28 11:34  漫天飞雪世情难却  阅读(112)  评论(0编辑  收藏  举报