Python if-else and while

if-elif语法

if condition:
[tab键] command
elif condition:
[tab键] command
elif condition:
[tab键] command
else:
[tab键] command


while语法

while condition:
[tab键] command


if嵌套

if condition:
[tab键] command
[tab键] if condition:
[tab键] [tab键] command
[tab键] else:
[tab键] [tab键] command
else:
[tab键] command
#-*- coding:utf-8 -*-

#双循环打印矩形

#矩形长度是7,宽度是5

i = 0
j = 0

while i < 5:
        j = 0
        while j < 7:
                if (i == 0) or (i == 4):
                        #这种end=""不换行操作是python3.x版本支持的,python2.x下会报错
                        print("*",end='')
                else:
                        if (j == 0) or (j == 6):
                                print("*",end = "")
                        else:
                                print(" ",end='')
                j += 1
        print("")
        i += 1

猜你喜欢

转载自www.cnblogs.com/zhanggaofeng/p/9220439.html