day-004--循环结构

循环结构

主要有两个1个是for in一个是while,跳出的是用break,单次跳出用continue

1.for 的格式
for x in range(101):
    sum += x
2.while 结构格式
while True:
    # do what you want to do 

3.上代码

row = int(input('请输入行数: '))
for i in range(row):
    for _ in range(i + 1):
        print('*', end='')
    print()


for i in range(row):
    for j in range(row):
        if j < row - i - 1:
            print(' ', end='')
        else:
            print('*', end='')
    print()

for i in range(row):
    for _ in range(row - i - 1):
        print(' ', end='')
    for _ in range(2 * i + 1):
        print('*', end='')
    print()

for i in range(2,8):
    print(i)
print('-------------------------')
for i in range(2,8,3):
    print(i)
print('-------------------------')
n = 0
while True:
    print('n is %d' % n)
    n = n+8
    if n >= 30:
        print('while test is over!')
        break
print('-------------------------')
n =0
while n <= 100:
    print('n is %d' % n)
    n = n+8
    if n >= 30:
        print('while test is over another!')
        break

结果:

D:\python\venv\Scripts\python.exe D:/python/day004.py
请输入行数: 2
*
**
 *
**
 *
***
2
3
4
5
6
7
-------------------------
2
5
-------------------------
n is 0
n is 8
n is 16
n is 24
while test is over!
-------------------------
n is 0
n is 8
n is 16
n is 24
while test is over another!

Process finished with exit code 0

4.range的用法可以range(num),range(beg,end),range(beg,end,step)

文集传送门 学习python100天


整个学习python100天的目录传送门


无敌分割线


再最后面附上大神的链接传送门

你可能感兴趣的:(day-004--循环结构)