图案形成:
练习:
1)
for row in range(5): for col in range(5-row): print(5-col,end=' ') print()
输出:
5 4 3 2 1 5 4 3 2 5 4 3 5 4 5
2)
no = 1 for row in range(5): for col in range(5-row): print(no, end=' ') no+=1 print()
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3)
for row in range(5): for col in range(5-(row+1)): print(col+1, end=' ') print()
输出:
1 2 3 4 1 2 3 1 2 1
4)
for row in range(5): for col in range(4-row): print(col+1, end=' ') print("* ", end= ' ') print()
输出:
1 2 3 4 * 1 2 3 * 1 2 * 1 * *
5)
for row in range(5): for col in range(4-row): print(col+1, end=' ') for col in range(5): print("* ",end=' ') print()
输出:
1 2 3 4 * * * * * 1 2 3 * * * * * 1 2 * * * * * 1 * * * * * * * * * *
6)
for row in range(5): for col in range(4-row): print(col+1, end=' ') for col in range(5): print(col,end=' ') print()
输出:
1 2 3 4 0 1 2 3 4 1 2 3 0 1 2 3 4 1 2 0 1 2 3 4 1 0 1 2 3 4 0 1 2 3 4
7)
for row in range(5): for col in range(4-row): print(" ", end=' ') for col in range(row+1): print(col+1,end=' ') print()
输出:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
8)
for row in range(5): for col in range(4-row): print("", end=' ') for col in range(row+1): print(col+1,end=' ') print()
输出:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
9)
for row in range(5): for col in range(4-row): print(" ", end=' ') for col in range(5): print(col+1,end=' ') print()
输出:
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
10)
for row in range(5): for col in range(row+1): print(col+1,end=' ') print()
输出:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
11)
for row in range(5): for star in range(4-row): print("*",end=' ') for col in range(row+1): print(col+1,end=' ') print()
输出:
* * * * 1 * * * 1 2 * * 1 2 3 * 1 2 3 4 1 2 3 4 5
12)
for row in range(5): for star in range(4-row): print(" ",end=' ') for col in range(row+1): print(5-col,end=' ') print()
输出:
5 5 4 5 4 3 5 4 3 2 5 4 3 2 1
13)
for row in range(5): for space in range(4-row): print(' ',end=' ') for col in range(row+1): print(5+col-row,end=' ') print()
输出:
5 4 5 3 4 5 2 3 4 5 1 2 3 4 5
以上就是Python Day- 使用循环、任务形成模式的详细内容,更多请关注php中文网其它相关文章!
版权声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系 yyfuon@163.com