[英]How to print a string in each line with some space from the first of the line?
I did not write a clear subject because I didn't know what to write about it, sorry first of all.我没有写一个明确的主题,因为我不知道该写什么,首先抱歉。
What I want to reach is:我想要达到的是:
a
a
a
a
a
a
a
a
a
With for
loop I think I can reach this, but what I try is totally wrong.使用
for
循环,我想我可以达到这个目的,但我尝试的是完全错误的。
Your increment
counter is useless in your code.您的
increment
计数器在您的代码中是无用的。
The way I would approach this is:我处理这个问题的方法是:
column_num = 0
for i in range(10): # rows
for j in range(10): # cols
if j == column_num:
print("a", end='')
else:
print(' ', end='')
print() # newline
column_num += 1
#then reverse:
column_num -= 1
for i in range(10): #rows
for j in range(10): #cols
if j == column_num:
print("a", end = '')
else:
print(' ', end = '')
print() #newline
column_num -= 1
Maybe you are locking for something like this.也许你正在锁定这样的东西。
a = 'a '
space = '\t'
for c in range(5):
print(space*c + a)
for c in range(5,0,-1):
print(space*c + a)
You can do it recursively:您可以递归地执行此操作:
def print_angle(s, start, end, direction):
indent = " "*start
print(f"{indent}{s}")
if start < end-1 and direction:
print_angle(s, start+1, end, 1)
elif start > 0:
print_angle(s, start-1, end, 0)
print_angle('a', 0, 5, 1)
This prints:这打印:
a
a
a
a
a
a
a
a
a
The idea here is:这里的想法是:
1. Pass the desired character to print - a in this case. 1.传所需的字符打印-一个在这种情况下。
2. Pass the starting point - 0 indents in this case. 2.通过起点 - 在这种情况下 0 缩进。
3. Pass the ending point - 5 total to go. 3.通过终点 - 共 5 关。
4. Pass direction in which to go - 1 up, 0 down. 4.传球方向 - 1 向上,0 向下。
5. Go recursively up until you reach the end, preserve direction, initially up. 5.递归向上直到到达终点,保持方向,最初向上。
6. Go recursively down until you reach 0 and exit. 6.递归向下直到到达 0 并退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.