流程控制if,while,for

if语句

什么是if语句
判断一个条件如果成立则做...不成立则做....
为何要有if语句
让计算机能够像人一样具有判断的能力

如何用if语句

语法1:

if 条件1:
  code1
  code2
  code3
  ………
age=18
if age != 18:
  print('你好啊小伙子')
  print('加个微信吧...')
  print('other code...')
例子

语法2:

if 条件:
  code1
  code2
  code3
  …………
else:
  code1
  code2
  …………
View Code
age=18
sex='male'
wuzhong='human'
is_beautiful=True

if age > 16 and age < 22 and sex == 'female' and 
wuzhong == 'human' and is_beautiful:
  print('开始表白...')
else:
  print('阿姨好,我逗你玩呢...')
例子

语法3:

if 条件1:
  code1
  code2
  code3
if 条件2:
  code2
  code3
View Code
a=88
if a>= 90:
    print u"优秀"
if a == 88:
    print u"正好88"
if  a >= 80:
    print u"良好"
else:
    print u"都不合格"
View Code

语法4:

if 条件1:
  子代码块1
elif 条件2:
  子代码块2
elif 条件3:
  子代码块3
elif 条件4:
  子代码块4
  .........
else:
  子代码块5
View Code
a=88
if a>= 90:
    print u"优秀"
elif a == 88:
    print u"正好88"
elif  a >= 80:
    print u"良好"
else:
    print u"都不合格"
View Code

关于if和elif要注意以下几点

如果你是想遍历到你的判断条件就不再执行其他判断条件分支语句,那么就用elif;

如果程序中判断事件很多,全部用if的话,不管你想判断的条件有没有遍历到,他都会继续执行完所有的if;用elif 程序运行时,只要if或后续某一个elif之一满足逻辑值为True,则程序执行完对应输出语句后面所有的elif和else就不会再被执行,会提高效率。

while循环

什么是循环
循环指的是一个重复做某件事的过程

为何要有循环
为了让计算机能够像人一样重复做某件事

如何用循环

while循环的语法:while循环又称为条件循环,循环的次数取决于条件

while 条件:
    子代码1
    子代码2
    子代码3
print('start....')
while True:
    name=input('please your name>>: ')
    pwd=input('please your password>>: ')
    if name == 'egon' and pwd == '123':
        print('login successful')
    else:
        print('user or password err')
print('end...')
View Code

如何结束while循环

方式一:操作while循环的条件让其结束

print('start....')
tag=True
while tag:
    name=input('please your name>>: ')
    pwd=input('please your password>>: ')
    if name == 'egon' and pwd == '123':
        print('login successful')
        tag=False
    else:
        print('user or password err')

print('end...')
View Code

方式二: break强行终止本层循环

count=1
while count < 6:
    print(count)
    count+=1


count=1
while True:
    if count > 5:
        break
    print(count)
    count+=1
View Code
print('start....')
while True:
    name=input('please your name>>: ')
    pwd=input('please your password>>: ')
    if name == 'egon' and pwd == '123':
        print('login successful')
        break
    else:
        print('user or password err')

print('end...')
View Code

输错三次则退出
方式一:

print('start....')
count=0
while count <= 2: #count=3
    name=input('please your name>>: ')
    pwd=input('please your password>>: ')
    if name == 'egon' and pwd == '123':
        print('login successful')
        break
    else:
        print('user or password err')
        count+=1
print('end...')
View Code

方式二:

View Code
while+continue:continue代表结束本次循环,直接进入下一次
count=1
while count < 6:
    if count ==  4:
        count+=1
        continue # 只能在cotinue同一级别之前加代码
    print(count)
    count+=1
View Code
while True:
    print('11111')
    print('22222')
    print('333')
    continue # 不应该将continue作为循环体最后一步执行的代码
View Code

while+else

意思是 while循环中,只要循环正常完成,即中途没有break那么就会 执行else语句,否则就不会
实际用处:提供了简便方法,判断循环有无被终止

输错三次则退出之while+else的应用

count=1
while count < 6:
    if count == 4:
        break
    print(count)
    count+=1
else:
    print('会在while循环没有被break终止的情况下执行')
View Code
print('start....')
count=0
while count <= 2: #count=3
    name=input('please your name>>: ')
    pwd=input('please your password>>: ')
    if name == 'egon' and pwd == '123':
        print('login successful')
        break
    else:
        print('user or password err')
        count+=1
else:
    print('输错的次数过多')

print('end...')
View Code

while循环的嵌套

name_of_db='egon'
pwd_of_db='123'
print('start....')
count=0
while count <= 2: #count=3
    name=input('please your name>>: ')
    pwd=input('please your password>>: ')
    if name == name_of_db and pwd == pwd_of_db:
        print('login successful')
        while True:
            print("""
            1 浏览商品
            2 添加购物车
            3 支付
            4 退出
            """)
            choice=input('请输入你的操作: ') #choice='1'
            if choice == '1':
                print('开始浏览商品....')
            elif choice == '2':
                print('正在添加购物车....')
            elif choice == '3':
                print('正在支付....')
            elif choice == '4':
                break
        break
    else:
        print('user or password err')
        count+=1
else:
    print('输错的次数过多')

print('end...')
View Code

tag控制所有while循环(相当于一个while循环的开关)

name_of_db='egon'
pwd_of_db='123'
tag=True
print('start....')
count=0
while tag:
    if count == 3:
        print('尝试次数过多')
        break
    name=input('please your name>>: ')
    pwd=input('please your password>>: ')
    if name == name_of_db and pwd == pwd_of_db:
        print('login successful')
        while tag:
            print("""
            1 浏览商品
            2 添加购物车
            3 支付
            4 退出
            """)
            choice=input('请输入你的操作: ') #choice='1'
            if choice == '1':
                print('开始浏览商品....')
            elif choice == '2':
                print('正在添加购物车....')
            elif choice == '3':
                print('正在支付....')
            elif choice == '4':
                tag=False

    else:
        print('user or password err')
        count+=1

print('end...')
View Code

for循环

for循环主要用于循环取值

student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']

i=0
while i < len(student):
    print(student[i])
    i+=1

for item in student:
    print(item)

for item in 'hello':
    print(item)

dic={'x':444,'y':333,'z':555}
for k in dic:
    print(k,dic[k])


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

for i in range(10):
    print(i)

student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']
for item in student:
    print(item)

for i in range(len(student)):
    print(i,student[i])
View Code

for循环的嵌套

打印九九乘法表
分析
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

用用python语法打印九九乘法表,这肯定会用到循环,在其他很多编程语言java、c、js等

都可以用 for 循环或者while循环 进行嵌套 从而实现 乘法表的打印。

但是在python中不能使用for循环,python中的for循环一般是用来遍历 python中的非数字类型 也就序列或者容器。

但是python中有range函数可以返回一个可迭代对象,可以用来计算,同样可以用来实现循环打印。

下面我们就用python语法中的while嵌套循环进行九九乘法表的打印。

主要注意一下几点就行了。

1、python中的print() 打印函数默认自带换行,可以添加 第二参数 end = "" 来阻止换行。

end代表要打印字符串的最后最后一个字符,可以换成制表符或者其他的符号代替换行符而已。

2、还有就是python中没有自增自减运算符!只能通过赋值运算符进行自增自减了。

3、python中的print中多个占位符要用()括起来,要注意这个语法。

4、对python来说格式也就是缩进非常重要,它根据缩进来判断是否是同一代码块的内容

代码实现

for i in range(1, 10):
    for j in range(1, i + 1):
        print('%s*%s=%s' % (j, i, i * j), end=' ')
    print()
View Code

打印金字塔

分析

#max_level=5
* #current_level=1,blank=4,*号数=1
*** #current_level=2,blank=3,*号数=3
***** #current_level=3,blank=2,*号数=5
******* #current_level=4,blank=1,*号数=7
********* #current_level=5,blank=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='')  # 在一行中连续打印多个空格 ,本来print自动换行的,但是 end= 限制了自动换行
    for j in range(2 * current_level - 1):
        print('*', end='')  # 在一行中连续打印多个空格
    print()
View Code
原文地址:https://www.cnblogs.com/596014054-yangdongsheng/p/9648840.html