The basic flow control

A variable immutable type

Variable type:

Value change, id unchanged, to prove the original value is changed, i.e., the original value can be changed

Immutable type:

Changes value, id has changed, a new proof values, There is not changing the original, the original value is not proven to be modified

1) int is immutable

2) float type is immutable

3) str is immutable

Summary: int, float, str value types are designed as a whole is split became impossible, it can not be changed

4) list is a variable type

5) dic is a variable type

Definition: within {} are separated by commas multiple key: value,

Wherein the value may be any type,

But the key must be immutable type

The dictionary is not hashed

6) bool is immutable

Two conditions

What is the condition? What can be used as conditions? Why do we use the conditions?

The first category: Explicit Boolean : True, False

Conditions can be: a comparison operator

        age = 18
        print(age > 16) #条件判断之后会得到一个布尔值

Conditions could be: True, False

        is_beatiful= True
        print(is_beatiful) #True

The second category: the implicit Boolean value : All values can be used as conditions to use,

Wherein 0, None, Empty (empty dictionary, an empty string, empty dictionary) = "represents the Boolean value is False, True rest are

========= lack ==============

If the three flow control

Syntax 1:

    if 条件:
            代码1
            代码2
            代码3

Syntax 2:

if 条件:
        代码1
else:
        代码2

Syntax 3:

if 条件:
        代码1
elif:
        代码2
else:
        代码3
    

Example:

score = input('请输入您的成绩:') # score="18"
score=int(score)

if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')
else:
    print('很差,小垃圾')

if nested:

age = 18
is_beautiful = True
star = '摩羯座'

if 16 < age < 20 and is_beautiful and star == '摩羯座':
    print('开始表白。。。。。')
    is_successful = True
    if is_successful:
        print('两个从此过上羞羞的生活。。。')
else:
    print('阿姨好!')
print('其他代码.............')

Four process control purposes while

The basic syntax using loops

print(1)
while 条件:
     代码1
     代码2
     代码3
print(3)

Death grammar and efficiency of the cycle

while True:
    name=input('your name >>>> ')
    print(name)

Io no pure calculation cycle of death will lead to a fatal efficiency

while True:
    1+1

Two ways to exit the loop

One way: to take effect when the conditions were changed to False, wait until the next cycle to determine the conditions

tag=True
while tag:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        tag = False # 之后的代码还会运行,下次循环判断条件时才生效
    else:
        print('账号名或密码错误')

    print('====end====')

Second way: break long runs to break the cycle will immediately terminate this layer

while True:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        break # 立刻终止本层循环
    else:
        print('账号名或密码错误')

    # print('====end====')

while the end of the loop nest

tag=True
while tag:
    while tag:
        while tag:
            tag=False
    

# 每一层都必须配一个break
while True:
    while True:
        while True:
            break
        break
    break
while True:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        while True:
            cmd=input("输入命令>: ")
            if cmd == 'q':
                break
            print('命令{x}正在运行'.format(x=cmd))
        break # 立刻终止本层循环
    else:
        print('账号名或密码错误')
    print('====end====')

Conditions change the way

tag=True
while tag:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        while tag:
            cmd=input("输入命令>: ")
            if cmd == 'q':
                tag=False
            else:
                print('命令{x}正在运行'.format(x=cmd))
    else:
        print('账号名或密码错误')

while + continue: the end of this cycle, directly into the next

Note: The code is meaningless to add the same level after continue, because never run

count=0
while count < 6:
    if count == 4:
        count+=1
        continue
        # count+=1 # 错误
    print(count)
    count+=1

while +else:针对break

count=0
while count < 6:
    if count == 4:
        count+=1
        continue
    print(count)
    count+=1
else:
    print('else包含的代码会在while循环结束后,并且while循环是在没有被break打断的情况下正常结束的,才不会运行')

Optimized version:

count=0
while count < 3:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        while True:
            cmd=input("输入命令>: ")
            if cmd == 'q': # 整个程序结束,退出所有while循环
                break
            else:
                print('命令{x}正在运行'.format(x=cmd))
        break
    else:
        print('账号名或密码错误')
        count+=1
else:
    print('输错3次,退出')

Guess you like

Origin www.cnblogs.com/xy-han/p/12450257.html