其他分享
首页 > 其他分享> > 流程控制之循环

流程控制之循环

作者:互联网

今日内容:
1、流程控制之if判断

2、流程控制之循环
    while循环-》“条件循环”
    for循环-》“取值循环”

流程控制即控制流程,具体指控制程序的执行流程
1、什么是if判断

    接收用户输入的用户名
    接收用户输入的密码
    判断 输入的用户名 等于 正确的用户名 并且 输入的密码 等于 正确的密码:
        告诉用户登录成功
    否则:
        告诉用户登录失败

2、为何要用if判断
    为了让计算机能够像人一样去判断

3、如何用if判断
一 if判断基本运行原理解析,完整语法

if 条件1:
    子代码
elif 条件2:
    子代码
elif 条件3:
    子代码
......
else:
    子代码
二:单分支
gender = "female"
age = 27
is_beautiful = True

if gender == "female" and 18 < age < 26 and is_beautiful:
    print("我喜欢,加个微信")
print('end.....')
三:双分支
gender = "female"
age = 27
is_beautiful = True

if gender == "female" and 18 < age < 26 and is_beautiful:
    print("我喜欢,加个微信")
else:
    print("咱俩不太合适")
print('end.....')

案例:
inp_user = input("请输入您的用户名:")
inp_pwd = input("请输入您的密码:")

if inp_user == "egon" and inp_pwd == "123":
    print("登录成功")
else:
    print("登录失败")
四:多分支
如果:成绩>=90,那么:优秀

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

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

其他情况:很差

score = input("请输入您的分数 ")
score = int(score)
if score >= 90:
    print("优秀")
elif score >= 80:
    print("良好")
elif score >= 70:
    print("普通")
else:
    print("很差")
gender = 'female'
age = 19
is_beautiful = True
if gender == 'female' and 18 < age < 25 and is_beautiful:
    print('加个微信')
inp_user = input('用户名')
inp_pwd = input('密码')
if inp_user == 'xxx' and inp_pwd == '123':
    print('登陆成功')
else:
    print('用户名或者密码错误')
score = input('请输入您的分数')
score = int(score)
if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')
else:
    print('很差')
i = 1
while i <= 5:
    print(i)
    i += 1
i = 1
tag = True
while tag:
    if i == 5:
        tag = False
    print(i)
    i += 1
i = 1
tag = True
while tag:
    if i == 5:
        break
    print(i)
    i += 1
i = 1
while i <= 5 :
    print(i)
    i += 1
num = 11
while num > 1:
    num -= 1
    if num == 7:
        continue
    print(num)
tag = True
while tag:
    username = input('请输入账号')
    password = input('密码')
    if username == 'xxx' and password == '123':
        print('登陆成功')
        tag = False
    else:
        print('登陆失败')
    print('....')
while True:
    username = input('请输入账号')
    password = input('密码')
    if username == 'xxx' and password == '123':
        print('登录成功')
        break
    else:
        print('登陆失败')
    print('....')
i = 1
while i <= 5:
    if i == 3:
        i += 1
        continue
    print(i)
    i += 1
nums = [111, 222, 333, 444, 555]
i = 0
while i < len(nums):
    print(nums[i])
    i += 1
nums = [111, 222, 333, 444, 555]
for i in nums:
    print(i)
dic = {'k1': 111, 'k2': '222', 'k3': 333}
for x in dic:
print(x, dic[x])
for i in range(1,100):
    print(i)
一 for更擅长遍历值,for循环的次数取决值的个数

nums = [111, 222, 333, 444, 555]

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

for num in nums:  # num = 222
    print(num)

for x in "hello":
    print(x)

dic = {'k1':111,'k2':222,'k3':333}
for k in dic:
    print(k,dic[k])

items = [['name', "egon"], ['age', 18], ['gender', "male"]]
for x,y in items:  # x,y = ['name', 'egon']
    print(x,y)
二 for+break
nums = [111, 222, 333, 444, 555]
for num in nums:
    if num == 333:
        break
    print(num)
三 for+continue
nums = [111, 222, 333, 444, 555]
for num in nums:
    if num == 333:
        continue
    print(num)
四 for+else
nums = [111, 222, 333, 444, 555]
for num in nums:
    if num == 333:
        # break
        continue
    print(num)
else:
    print('======')
五
让某段代码重复运行3次-》while循环实现如下
i = 0
while i < 3:
    print('hello1')
    print('hello2')
    print('hello3')
    i += 1

让某段代码重复运行3次-》for循环实现如下
for x in range(3):
    print('hello1')
    print('hello2')
    print('hello3')

for x in range(100):
    print(x)
六 for+enumerate
nums = [111, 222, 333, 444, 555]

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

for i,num in enumerate(nums):
    print(i,num)

标签:控制,num,nums,流程,333,else,循环,print,input
来源: https://www.cnblogs.com/echoxkx/p/15079578.html