Python 流程控制

今日目录:

一、流程控制

  • 1. if
  • 2. while
  • 3. for
  • 4. 后期补充内容

一、流程控制--if

1.if判断:
# if判断
age = 21
weight = 50

if age > 18 and age < 25 and weight >40 and weight < 60:
    print("表白。。。")
View Code
2.if+else:
# 语法二:if - else
age = 21
weight = 50

if age > 18 and age < 25 and weight > 40 and weight < 60:
    print("表白。。。")
else:
    print("你好!再见!")
View Code 
3.if嵌套:
# 语法三:if条件的嵌套
sex = "female"
age = 18
weight = 50
is_successful = True

if sex == "female" and age >= 18 and age < 25 \
        and weight > 40 and weight < 60 :
    print("表白。。。")
    if is_successful:
        print("在一起、、、、")
    else:
        print("qtmd、、、、、")
else:
    print("你好!再见!")
View Code

 

4.if-elif:
如果 成绩>=90,那么:优秀

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

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

其他情况:很差
score = input("请输入你的成绩:")

score = int(score)

if score >= 90:
    # print("优秀!")
    print("你真TM优秀!")

elif score >= 80:
    # print("良好!")
    print("恩,还不错!")

elif score >= 70:
    # print("普通!")
    print("一般般啦!")
else:
    # print("很差!")
    print("回家卖红薯吧!")
View Code

 

二、流程控制--while

1.while循环又称为条件循环
while True:
    name = input("Please input your name :")
    pwd = input("Please input your password :")

    if name == "zhangsan" and pwd =="123":
        print("login。。。。。。")
    else:
        print("Your username or password error , please input again!")
View Code

 

2.while+条件结束
# ---条件跳出--
tag = True
while tag:
    name = input("Please input your name :")
    pwd = input("Please input your password :")

    if name == "zhangsan" and pwd =="123":
        print("login。。。。。。")
        tag = False
    else:
        print("Your username or password error , please input again!")
View Code

 

3.while+break:
break:退出本层循环
# ---break-----
while True:
    name = input("Please input your name :")
    pwd = input("Please input your password :")

    if name == "zhangsan" and pwd =="123":
        print("login。。。。。。")
        break
    else:
        print("Your username or password error , please input again!")
View Code
4.while+continue:
continue:退出本次循环,继续下一次循环
# ---continue----
count = 1
while count < 6:
    if count == 4:
        count += 1
        continue

    print(count)
    count += 1
View Code 
5.while + else
while循环正常执行完,中间没有break中止的话,执行else
# ---while-else----
count = 1
while count < 6:
    if count == 4:
        count += 1
        continue

    print(count)
    count += 1
else:
    print("while-else最后else的代码")
View Code

 

 

三、流程控制--for

for循环又称为迭代循环
for可以不依赖于索引取指,是一种通用的循环取指方式 循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的
for i in range(10):
    print(i)
View Code
1.for+break:
while类似
2.for+continue:
while类似
3.for+else:
while类似
4.range():
range(1,5)  
>>>1 2 3 4

range(5)
>>>1 2 3 4

range(1,5,2)  #i = i + 2
>>>1 3
	
注意:
  python2中:range(1,5)直接生成[1,2,3,4]列表
  python3中:range(1,5)不会直接生成列表,依旧是 range(1,5)
5.嵌套循环
# for嵌套循环    
for i in range(3):
    for j in range(3):
        print(i,j)

结果:
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
View Code

 

 

本节练习:

一、while循环

1.用户登录验证

# 1.用户登录验证

name = input("请输入用户名字: ")
pwd  = input("请输入密码: ")

if name =='zzw' and pwd =="123":
    print("zzw login success!")
else:
    print("用户名或密码错误!")
View Code

 

2.根据用户输入内容输出其权限

# 2.根据用户输入内容输出其权限

# 
#        zzw    -- 超级管理员
#     zhangsan  -- 普通管理员
#        lisi   -- 业务主管
#        其他   -- 普通用户
#

name = input("请输入用户名字:")

if name == "zzw":
    print("超级管理员")
elif name == "zhangsan":
    print("普通管理员")
elif name == "lisi":
    print("业务主管")
else:
    print("普通用户")
View Code

 

3.浪否?

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

today = input("请输入今天是周几:")

if today in ["Monday","Tuesday","Wednesday","Thurday","Friday"]:
    print("滚去上班!不准浪!")
elif today in ["Saturday","Sunday"]:
    print("出去浪吧!")
else:
    print("""
    输入格式:
        Monday,Tuesday,Wednesday,Thurday,Friday,Saturday,Sunday
    """)
View Code

 

二、while循环

1. 使用while循环输出1 2 3 4 5 6     8 9 102. 求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,就退出程序
如何猜对了,就直接退出
# 1.使用while循环输出1 2 3 4 5 6     8 9 10
i = 1
while i < 10:
    if i == 7:
        i += 1
        continue
    print(i,end=' ')
    i += 1
View Code
#2. 求1-100的所有数的和
sum = 0
i = 1
while i<=100:
    sum +=i
    i +=1
print(sum)
View Code
#3.输出 1-100 内的所有奇数

i = 1
while i<=100:
    print("%s"%i,end=" ")
    i += 2
View Code
#4.使用 while 循环实现输出 1-100 内的所有偶数

i = 1
while i<=100:
    print("%s" % (i+1), end=" ")
    i += 2
View Code
# 5.使用while循环实现输出2-3+4-5+6...+100 的和
i = 2
sum = 0
tag = True  # 符号判断位,用来控制加减 True: +i  False: -i
while i <= 100:
    if tag:
        sum += i
        tag = False
    else:
        sum -= i
        tag = True
    i += 1
print("2-3+4-5+...+100 = %s" % sum)
View Code
#6. 用户登陆(三次机会重试)
count = 0
tag = True

while tag:
    if count >= 3:
        break
    name = input("Please input your name :")
    pwd = input("Please input your password :")

    if name == "zhangsan" and pwd =="123":
        print("login。。。。。。")
        tag = False
    else:
        print("Your username or password error , please input again!")
    count += 1
View Code
# #7:猜年龄游戏
# 要求:
#     允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
AGE = 56
tag = True
count = 0

while tag:
    age = input("请猜一猜年龄:")
    if age == AGE:
        print("这都能猜中!太厉害了!")
        tag = False
    else:
        print("很遗憾,你猜错了!")

    if count ==2:
        print("你都猜了三次了,竟然还没猜对!!")
        break
    count +=1
View Code
# #8:猜年龄游戏升级版
# 要求:
#     允许用户最多尝试3次
#     每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
#     如何猜对了,就直接退出

AGE = 56
tag = True
count = 0

while tag:
    age = input("请猜一猜年龄:")
    if age == AGE:
        print("这都能猜中!太厉害了!")
        tag = False
    else:
        print("很遗憾,你猜错了!")
    count += 1

    if count ==3:
        is_continue = input("你还要继续猜吗?(Y/N)  :")
        if is_continue == 'Y' or is_continue == 'y':
            count = 0
        else:
            tag = False
View Code

 

三、for循环

1.打印九九乘法口诀表
以下是 print() 方法的语法:
    
    print(*objects, sep=' ', end='\n', file=sys.stdout)
    参数
    objects -- 复数,表示可以一次输出多个对象。输出多个对象时,需要用 , 分隔。
    sep -- 用来间隔多个对象,默认值是一个空格。
    end -- 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符串。
    file -- 要写入的文件对象。

 

输出结果:

  1*1=1
  1*2=2 2*2=4
  1*3=3 2*3=6 3*3=9
  1*4=4 2*4=8 3*4=12 4*4=16
  1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
  1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
  1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
  1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
  1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

for i in range(1, 10):
    for j in range(1, i + 1):
        print("%s*%s=%s" % (i, j, i * j), end=' ')
    print()  # 内循环进行完一次,换行
View Code

 


2.打印金字塔
 输出结果:
* *** ***** ******* *********
# 打印金字塔

row = 5
col = 9

for i in range(1,row+1):  # 5行
    num = (2*i-1)  # 此行将打印几个 *
    for l in range((col-num)//2):  # 打印前面几个空格符
        print(" ",end="")
    for k in range((col-num)//2,(col-num)//2 + num):  # 在哪个位置开始打印 *
        print("*",end='')
    print()
View Code

 

四、后期补充内容

4.1 流程控制 -菜鸟教程

条件控制:菜鸟教程

循环语句:菜鸟教程

 

posted @ 2019-03-21 17:27  xt12321  阅读(379)  评论(0编辑  收藏  举报