2024年4月8日 星期一

Python雙迴圈基礎教材

 1.

1
2
3
4
for i in range(5):
    for j in range(i+1):
        print('*', end='')
    print()

執行結果:



2.
1
2
3
4
for i in range(5):
    for j in range(5-i):
        print('*', end='')
    print()

執行結果:



3.

1
2
3
4
5
6
7
8
for i in range(5):
    for j in range(i+1):
        print('*', end='')
    print()
for i in range(5):
    for j in range(5-i-1):
        print('*', end='')
    print()

執行結果:


4.

1
2
3
4
5
6
for i in range(5):
    for j in range(5-i-1):
        print(' ', end='')
    for j in range(i+1):
        print('*', end='')
    print()

執行結果:

5.

1
2
3
4
5
6
for i in range(5):
    for j in range(i):
        print(' ', end='')
    for j in range(5-i):
        print('*', end='')
    print()

執行結果:


6.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
for i in range(5):
    for j in range(5-i-1):
        print(' ', end='')
    for j in range(i+1):
        print('*', end='')
    print()
for i in range(5):
    for j in range(i+1):
        print(' ', end='')
    for j in range(5-i-1):
        print('*', end='')
    print()

執行結果:

7.

1
2
3
4
5
6
for i in range(5):
    for j in range(5-i-1):
        print(' ', end='')
    for j in range(i*2+1):
        print('*', end='')
    print()

執行結果:


8.

1
2
3
4
5
6
for i in range(5):
    for j in range(5-i-1):
        print(' ', end='')
    for j in range(i*2+1):
        print(str(i+1), end='')
    print()

執行結果:

9.

1
2
3
4
5
6
for i in range(5):
    for j in range(5-i-1):
        print(' ', end='')
    for j in range(i*2+1):
        print(str(j+1), end='')
    print()

執行結果:


沒有留言:

張貼留言