Python 入门基础3 ---流程控制

一、流程控制--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. 使用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 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=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


猜你喜欢

转载自www.cnblogs.com/xt12321/p/10571845.html
今日推荐