Python流程控制

1 条件控制(分支结构)

1.2 语法格式

#单向分支
if condition:
     statement_block

#双向分支
if condition:
     statement_block1
else:
     statement_block2

#多向分支
if condition_1:
    statement_block_1
elif condition_2:
    statement_block_2
else:
    statement_block_3

# 嵌套分支
if condition:
    if condition_1:
        pass
       else:
        pass

1.2 分支结构练习

1 如果:对面女孩的年龄>30岁,那么:叫大姐姐

age_of_girl=31
if age_of_girl > 30:
    print('大姐姐好')

2 如果:对面女孩的年龄>30岁,那么:叫阿姨,否则:叫小姐

age_of_girl=18
if age_of_girl > 30:
    print('大姐姐好')
else:
    print('小姐好')

3 如果:对面女孩的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:叫阿姨

age_of_girl=18
height=171
weight=99
is_pretty=True
if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True:
    print('表白...')
else:
    print('阿姨好')
#在表白的基础上继续:
#如果表白成功,那么:在一起
#否则:打印。。。

age_of_girl=18
height=171
weight=99
is_pretty=True

success=False

if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True:
    if success:
        print('表白成功,在一起')
    else:
        print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊...')
else:
    print('大姐姐,不约')

4 如果:成绩>=90,那么:优秀

如果成绩>=80且<90,那么:良好

如果成绩>=70且<80,那么:普通

其他情况:很差

score=input('>>: ')
score=int(score)

if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')
else:
    print('很差')

5 练习:模拟登陆

#!/usr/bin/env python

name=input('请输入用户名字:')
password=input('请输入密码:')

if name == 'egon' and password == '123':
    print('egon login success')
else:
    print('用户名或密码错误')

6 练习:根据用户输入打印权限

#!/usr/bin/env python
#根据用户输入内容打印其权限

'''
admin --> 超级管理员
tom  --> 普通管理员
jack,rain --> 业务主管
其他 --> 普通用户
'''
name=input('请输入用户名字:')

if name == 'amin':
    print('超级管理员')
elif name == 'tom':
    print('普通管理员')
elif name == 'jack' or name == 'rain':
    print('业务主管')
else:
    print('普通用户')

7 练习:星期几判断

# 如果:今天是Monday,那么:上班
# 如果:今天是Tuesday,那么:上班
# 如果:今天是Wednesday,那么:上班
# 如果:今天是Thursday,那么:上班
# 如果:今天是Friday,那么:上班
# 如果:今天是Saturday,那么:出去浪
# 如果:今天是Sunday,那么:出去浪


#方式一:
today=input('>>: ')
if today == 'Monday':
    print('上班')
elif today == 'Tuesday':
    print('上班')
elif today == 'Wednesday':
    print('上班')
elif today == 'Thursday':
    print('上班')
elif today == 'Friday':
    print('上班')
elif today == 'Saturday':
    print('出去浪')
elif today == 'Sunday':
    print('出去浪')
else:
    print('''必须输入其中一种:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    ''')

#方式二:
today=input('>>: ')
if today == 'Saturday' or today == 'Sunday':
    print('出去浪')

elif today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' \
    or today == 'Thursday' or today == 'Friday':
    print('上班')

else:
    print('''必须输入其中一种:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    ''')


#方式三:
today=input('>>: ')
if today in ['Saturday','Sunday']:
    print('出去浪')
elif today in ['Monday','Tuesday','Wednesday','Thursday','Friday']:
    print('上班')
else:
    print('''必须输入其中一种:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    ''')

1.3 三元表达式

name=input('姓名>>: ')
res='美女' if name == '丹丹' else '丑逼'
print(res)

2 循环语句 - while

2.1 为何要用循环

上节课我们已经学会用if .. else 来猜年龄的游戏啦,但是只能猜一次就中的机率太小了,如果我想给玩家3次机会呢?就是程序启动后,玩家最多可以试3次,这个怎么弄呢?你总不会想着把代码复制3次吧。。。。

2.2 while 用法

while 判断条件:
    语句
#打印0-10
count=0
while count <= 10:
    print('loop',count)
    count+=1

#打印0-10之间的偶数
count=0
while count <= 10:
    if count%2 == 0:
        print('loop',count)
    count+=1

#打印0-10之间的奇数
count=0
while count <= 10:
    if count%2 == 1:
        print('loop',count)
    count+=1

2.3 死循环

import time
num=0
while True:
    print('count',num)
    time.sleep(1)
    num+=1

2.4 循环与嵌套

tag=True 

  while tag:

    ......

    while tag:

      ........

      while tag:

        tag=False

练习:

1 循环验证用户输入的用户名与密码

2 认证通过后,运行用户重复执行命令

3 当用户输入命令为quit时,则退出整个程序

name='admin'
password='123'

tag=True
while tag:
    inp_name=input('用户名: ')
    inp_pwd=input('密码: ')
    if inp_name == name and inp_pwd == password:
        while tag:
            cmd = input('>>:')

            if cmd == 'quit':
                tag = False
            else:
                print('run %s' %cmd)
    else:
        print('用户名或密码错误')

2.5 break 和 continute

#break用于退出本层循环
while True:
    print "123"
    break
    print "456"

#continue用于退出本次循环,继续下一次循环
while True:
    print "123"
    continue
    print "456"

练习:结合break和continue 实现上个小节的需求

name='admin'
password='123'

while True:
    inp_name=input('用户名: ')
    inp_pwd=input('密码: ')
    if inp_name == name and inp_pwd == password:
        while True:
            cmd=input('>>: ')
            if not cmd:continue
            if cmd == 'quit':
                break
            print('run <%s>' %cmd)
    else:
        print('用户名或密码错误')
        continue
    break

while else

#与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句,while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句
count = 0
while count <= 5 :
    count += 1
    print("Loop",count)

else:
    print("循环正常执行完啦")
print("-----out of while loop ------")


#如果执行过程中被break啦,就不会执行else的语句啦
count = 0
while count <= 5 :
    count += 1
    if count == 3:break
    print("Loop",count)

else:
    print("循环正常执行完啦")
print("-----out of while loop ------")

pass 语句

Python pass是空语句,是为了保持程序结构的完整性。

while循环练习 作业

#1. 使用while循环输出1 2 3 4 5 6     8 9 10
#2. 求1-100的所有数的和
#3. 输出 1-100 内的所有奇数
#4. 输出 1-100 内的所有偶数
#5. 求1-2+3-4+5 ... 99的所有数的和
#6. 用户登陆(三次机会重试)
#7:猜年龄游戏
    要求:
    允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
#8:猜年龄游戏升级版 
    要求:
    允许用户最多尝试3次
    每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
    如何猜对了,就直接退出

3 循环语句 - for

3.1 用法

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

for <variable> in <sequence>:
    <statements>
else:
    <statements>

支持 for ... else

支持 break 和 continue

3.2 range

如果你需要遍历数字序列,可以使用内置range()函数。它会生成数列

# 遍历 0 到 4
for i in range(5):
    print(i)

# 遍历 5 到 8
for i in range(5,9):
    print(i)

也可以使range以指定数字开始并指定不同的增量

for i in range(0, 10, 3) :
    print(i)


for i in range(-10, -100, -30) :
    print(i)

3.3 for循环练习

打印九九乘法表

1x1=1 
1x2=2 2x2=4 
1x3=3 2x3=6 3x3=9 
1x4=4 2x4=8 3x4=12 4x4=16 
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
for i in range(1,10):
    for j in range(1,i+1):
        print('%s*%s=%s' %(i,j,i*j),end=' ')
    print()

打印金字塔

#分析
'''

             #max_level=5
    *        #current_level=1,空格数=4,*号数=1
   ***       #current_level=2,空格数=3,*号数=3
  *****      #current_level=3,空格数=2,*号数=5
 *******     #current_level=4,空格数=1,*号数=7
*********    #current_level=5,空格数=0,*号数=9

#数学表达式
空格数=max_level-current_level
*号数=2*current_level-1

'''

#实现
max_level=5
for current_level in range(1,max_level+1):
    for i in range(max_level-current_level):
        print(' ',end='') #在一行中连续打印多个空格
    for j in range(2*current_level-1):
        print('*',end='') #在一行中连续打印多个空格
    print()

results matching ""

    No results matching ""