python全栈学习day04if判断、while循环、for循环

#1.流程控制之if判断
1 什么是if判断

判断一个条件如果成立则做…不成立则做…

2 为何要有if判断

让计算机能够像人一样具有判断的能力

3 如何用if判断
if … else

单分支

#if条件: 
#    满足条件后要执行的代码
age_of_oldboy = 56

if age_of_oldboy > 50:
    print('Too old,time to retire..')

双分支

#if条件: 
#    满足条件后要执行的代码
#else:
     if条件不满足就走这条

age_of_oldboy = 48
if age_of_oldboy > 50:
    print('Too old,time to retire..')
else:
    print('还能折腾几年')

缩进(知识点)

python的一大特色,强制缩进,目的是为了让程序知道,每段代码依赖哪个条件,如果不通过缩进来区分,程序怎么会知道,当你的条件成立后,去执行哪些代码?

python的缩进有以下几个原则:

  • 顶级代码必须顶行写,即如果一行代码本身不依赖于任何条件,那它必须不能进行任何缩进
  • 同一级别的代码,缩进必须一致
  • 官方建议缩进用4个空格

多分支

#if条件:
#    满足上面执行代码
#elif条件:
#    上面的条件不满足执行这个
#elif条件:
#    上面的条件不满足执行这个
#elif条件:
#    上面的条件不满足执行这个
#else:
#    上面的条件不满足执行这个

age_of_oldboy = 30
guess = int(input('>>:'))

if guess > age_of_oldboy:
    print('猜的太大了,往小试试...')
elif guess < age_of_oldboy:
    print('猜的太小了,往大试试...')
elseprint('恭喜你,猜对了')

#2.流程控制之while循环
1. 什么是循环

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

2. 为何要有循环

为了让计算机能够像人一样重复做某件事

3. 如何用循环

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

#while条件:
#    子代码1
#    子代码2
#    子代码3
print('start...')
while True:
    name = input('please your name:')
    pwd = input('please your pwd:')
    if name == 'wood'and pwd == '123':
        print('login successful')
    else:
        print('user or password err')
print('Its over------------')

有一种循环叫死循环,一点开始执行,就运行到天荒地老、海枯石烂
只要while的条件一直是True,它就会一直循环运行下去


当我们执行上面那组代码是,会发现一直在循环要求输入用户名和密码,不管输入的对不对。而我们要让它在输对的情况下,要停下来(结束循环)

  • 结束循环的两种方法
#方式1:操控while循环的条件让它结束循环
print('start...')
tag = True
while tag:    #用一个tag变量来代替True
    name = input('please your name:')
    pwd = input('please your pwd:')
    if name == 'wood'and pwd == '123':
        print('login successful')
        tag = False     #如果输入判定正确,变量tag值变成False,下一次循环条件不满足循环结束
    else:
        print('user or password err')
print('Its over------------')

#方式2:break强行终止本层循环
print('start...')
while True:
    name = input('please your name:')
    pwd = input('please your pwd:')
    if name == 'wood'and pwd == '123':
        print('login successful')
        break    #如果输入正确,直接强行终止循环
    else:
        print('user or password err')
print('Its over------------')

再思考,我们平常登录账号,输入账号密码,我们会不会不管输错多少次都让用户重新输入,不会,一般我们会允许输错3次,多了就不让输了。

#输错三次退出
#方式1:
print('start...')
count = 0
while count < 3:     #在while 条件上做判断
    name = input('please your name:')
    pwd = input('please your pwd:')
    if name == 'wood'and pwd == '123':
        print('login successful')
        break    #如果输入正确,直接强行终止循环
    else:
        print('user or password err')
    count += 1
print('Its over------------')

#方式2:
print('start...')
count = 0
while True:
    if count == 3:         #写一个if判断,用break终止循环
        print('错误次数太多')
        break
    name = input('please your name:')
    pwd = input('please your pwd:')
    if name == 'wood'and pwd == '123':
        print('login successful')
        break    #如果输入正确,直接强行终止循环
    else:
        print('user or password err')
        count += 1
print('Its over------------')

上面我们提到了有一个循环终止语句break(暴力终止,强行终止本层循环),还有一种语句continue(代表结束本次循环,直接进入下一次)

count = 0
while count < 5:
    count += 1
    if count == 4:
        continue    #结束本次循环,直接进入下一次
    print(count)

while+else用法

count = 0
while count < 5:
    count += 1
    print(count)
else:
    print('会在while语句没有被break终止的情况下执行')

_______________________________________________________
count = 0
while True:
    if count < 5:
        count += 1
        print(count)
    else:
        break      #while...else后的子代码不会执行
else:
    print('会在while语句没有被break终止的情况下执行')

之前登录允许错三次的while…else…用法:

print('start...')
count = 0
while count < 3 :
    name = input('please your name:')
    psw = input('please your psw:')
    if name == 'wood' and psw == '123':
        print('login succesful')
        break
    else:
        print('user or password err')
        count += 1
else:
    print('错误次数过多')
print('Its over...')

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只能终止本层循环
        break
    else:
        print('user or password err')
        count+=1
else:
    print('输错的次数过多')

print('end...')

———————————————————————————————————

作为牛逼的程序员,你不觉得每个 ‘while循环’ 都要用一个’break’来终止太low了吗?


# tag控制所有while循环

name_of_db='wood'
pwd_of_db='123'
print('start...')
count = 0
tag = True   #tag控制所有while循环
while tag:
    if count == 3:
        print('错误次数3次,遛了')
        break
    name = input('please your name:')
    psw = input('pleaase your psw:')
    if name == name_of_db and psw == pwd_of_db:
        print('login successful')
        while tag:
            print('1.浏览商品\
                   2.添加购物车\
                   3.支付\
                   4.退出')
            choice = input('请输入你的选择:')
            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('Its over...')

#3.流程控制之for循环

for循环主要用于取值

students = ['wood','egon','xjxdsb','zrdsb','lhpdsb']
#while循环取值
i = 0
while i < len(students):
    print(students[i])
    i += 1
___________________________________________________________________

students = ['wood','egon','xjxdsb','zrdsb','lhpdsb']
#for循环取值
for item in students:     
    print(students[item])
    
for item in 'wood':
    print(item)     #字符串它是个串也可以取值

students = {'name':'wood','age':'20','hobbies':['play','learning']}

for item in students:         #item取出来是key
    print(item,students[item])

#4.数据类型及内置方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值