7月2日内容

今天大概流程控制这方面内容。

1.1 if判断

  对事物的一种决策,判断事物对错的能力,而做出不同的响应。生活中的例子比比皆是,取款机取钱都会遇到选择的情况。

  if语句的形式为:a.             if  条件:

                           代码1

                        代码2

                         ......

  代码块是同一缩进级别的代码,上式中代码1和代码2是相同缩进的代码,这两个代码组合在一起就是一个代码块,相同缩进的代码自上而下运行。如你走在大街上,一个美丽且楚楚动人的妹子从你身边经过,这时你的大脑就会产生一些逻辑想法,要不要上去加微信,亦或者要不要上去表白。电脑

也是跟人一样会产生像这样的逻辑在处理事情的时候。

age = 27
is_beauty = True
if age>18 and age<30 and is_beauty:
    print('你好,妹子,哥很欣赏你,加微信可以否')
print('end...')

 输出结果为: 

 b.     if...else...

  其形式为:

if    条件:
        代码1
        代码2
        ......
else:
        代码1
        代码2
        ......

  用完整的代码可表示为:

age = 27
is_beauty = False
if age>18 and age<30 and is_beauty:
    print('你好,妹子,哥很欣赏你,加微信可以否')
else:
    print('滚犊子')
print('end...')

     执行结果为:

  c. if...elif...else

  形式为:

if 条件1:
    代码1
    代码2
    代码3
    ...
elif 条件2:
    代码1
    代码2
    代码3
    ...
else:
    代码1
    代码2
    代码3
    ...

  用完整的代码表示为:

age = 27
is_beauty = False
if age>18 and age<30 and is_beauty:
    print('你好,妹子,哥很欣赏你,加微信可以否')
elif age>24 and age<35:
    print('再考虑下')
else:
    print('滚犊子')
print('end...')

  运行结果为:

1.2 if嵌套

  我用一张图大致的汇总了if嵌套的规律,下图所示:

                                                                                 

  例如:

  
cls = 'human'
gender = 'female'
age = 18
is_success = False

if cls == 'human' and gender == 'female' and age > 16 and age < 22:
    print('开始表白')
    if is_success:
        print('那我们一起走吧...')
    else:
        print('我逗你玩呢')
else:
    print('阿姨好')
View Code

2.1 while 循环 

  实际生活中类似于重复的做一些事情,流水线上的工人反复劳动,直到下班时间到来。程序中需不需要做重复的事情呢?以刚刚的验证用户名和密码的例子为例,用户无论输入对错,程序都会立马结束,真正不应该是这个样子。。。

  while 语句的形式:a.

1 while 条件
2     code 1
3     code 2
4     code 3
5     ...
View Code

  非常注意:while  True:

          1+1

  这样的代码会出现死循环。

user_db = 'nick'
pwd_db = '123'
while True:
    inp_user = input('username: ')
    inp_pwd = input('password: ')
    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
    else:
        print('username or password error')

  用户输错了确实能够获取重新输,但用户输对了发现还需要输,这我就忍不了,接下来我会在这个基础上再进行深一步的研究。

  b.while + break 

  break的意思是终止掉当前层的循环,执行其他代码。break就近终止循环。

 1 user_db = 'jason'
 2 pwd_db = '123'
 3 while True:
 4     inp_user = input('username: ')
 5     inp_pwd = input('password: ')
 6     if inp_user == user_db and pwd_db == inp_pwd:
 7         print('login successful')
 8         break
 9     else:
10         print('username or password error')
11 print('退出了while循环')
View Code

  c.break + continue

  continue的意思是终止本次循环,直接进入下一次循环。

n = 1
while n < 10:
    if n == 6:
        n += 1  # 如果注释这一行,则会进入死循环
        continue
    print(n)
    n += 1
View Code

2.2 while 嵌套

 1 user_db = 'jason'
 2 pwd_db = '123'
 3 flag = True
 4 while flag:
 5     inp_user = input('username: ')
 6     inp_pwd = input('password: ')
 7     if inp_user == user_db and pwd_db == inp_pwd:
 8         print('login successful')
 9         while flag:
10             cmd = input('请输入你需要的命令:')
11             if cmd == 'q':
12                 flag = False
13                 break
14             print('%s功能执行'%cmd)
15     else:
16         print('username or password error')
17 print('退出了while循环')
View Code

3.for 循环

info = {'name': 'jason', 'age': 19}
for item in info:
    print(item)  # 拿到字典所有的key
    print(info[item])

  for循环不依赖索引号。for可以不依赖于索引取指,是一种通用的循环取指方式 , for的循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的。

  for循环也可以按照索引取值。

name_list =  ['jason', 'nick', 'tank', 'sean']
# for i in range(0,5):  # 5是数的
for i in range(len(name_list)):
    print(i, name_list[i])

  

 

        

  

 

  

猜你喜欢

转载自www.cnblogs.com/panshao51km-cn/p/11122705.html