分支&循环

    分支    

单分支

if 条件:
    满足条件后要执行的代码

双分支

if 条件:
    满足条件执行代码
else:
    if条件不满足就走这段

多分支:

if 条件:
    满足条件执行代码
elif 条件:
    上面的条件不满足就走这个
elif 条件:
    上面的条件不满足就走这个
elif 条件:
    上面的条件不满足就走这个    
else:
    上面所有的条件不满足就走这段

    while循环    

语法:

条件为真时执行循环体代码,条件为假时跳出循环。
while  条件:
    执行代码...

死循环:条件永远为真。

while True: 

    print(1)

循环中止语句:

break用于完全结束一个循环,跳出循环体执行循环后面的语句
continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环

while...else:

  当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

param = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
last_character = ['1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2', '1']
n, index = 0, 0

while n < 17:
    ipt = input('enter a number:').strip()
    if ipt.isdigit() and 0 <= int(ipt) <= 9:
        index += int(ipt) * param[n]
        n += 1
    else:
        print("非法输入!")
        break
else:
    index %= 11
    print('The last:', last_character[index])
View Code

    for循环    

  对可迭代对象进行遍历。

for i in range(10):
    print(i)
View Code
原文地址:https://www.cnblogs.com/webc/p/9512957.html