我不知道如何使用“#'s”和“*'s”在 python 中制作这个形状
I don't know how to make this shape in python using "#'s" and "*'s"
我正在尝试创建一个简单的 python 代码,它将使用“#”和“*”创建相当简单的形状,我对 python 很陌生,并且只想使用 for loops
和 range()
函数。我的代码可能比它需要的更复杂,但无论如何,这是我目前的代码:
Shapes = int(input("please enter the number size of the shape you want"))
sShapes = input("Would you like a square, triangle, Triangle 2 or Pyramid?")
s = 0
ss = Shapes + 1
##This is the code to make a square##
if sShapes == "Square":
for i in range(0, Shapes):
print('*'*Shapes)
##This is the code to make a triangle##
elif sShapes == "Triangle":
for b in range(0, ss, 1):
print('*'*(s+1)*b)
##This is the code that does not work##
elif sShapes == "T2":
for c in range(ss,0,-1):
print('#'*(s+1)*c, '*')
不起作用的代码应该如下所示:
####*
###**
##***
#****
*****
好吧,这是我打印上述图案的方法
n = 6
for i in range(1, n):
print("{}{}".format("#"*(n-i-1),"*"*i))
####*
###**
##***
#****
*****
您可以传递 n
而不是绝对数字
您可以使用单个变量来控制循环:
line_shape_number = 5 # Total number of shapes
for step in range(1, line_shape_number + 1):
print '{}{}'.format('#' * (line_shape_number - step), '*' * step)
我正在尝试创建一个简单的 python 代码,它将使用“#”和“*”创建相当简单的形状,我对 python 很陌生,并且只想使用 for loops
和 range()
函数。我的代码可能比它需要的更复杂,但无论如何,这是我目前的代码:
Shapes = int(input("please enter the number size of the shape you want"))
sShapes = input("Would you like a square, triangle, Triangle 2 or Pyramid?")
s = 0
ss = Shapes + 1
##This is the code to make a square##
if sShapes == "Square":
for i in range(0, Shapes):
print('*'*Shapes)
##This is the code to make a triangle##
elif sShapes == "Triangle":
for b in range(0, ss, 1):
print('*'*(s+1)*b)
##This is the code that does not work##
elif sShapes == "T2":
for c in range(ss,0,-1):
print('#'*(s+1)*c, '*')
不起作用的代码应该如下所示:
####*
###**
##***
#****
*****
好吧,这是我打印上述图案的方法
n = 6
for i in range(1, n):
print("{}{}".format("#"*(n-i-1),"*"*i))
####*
###**
##***
#****
*****
您可以传递 n
而不是绝对数字
您可以使用单个变量来控制循环:
line_shape_number = 5 # Total number of shapes
for step in range(1, line_shape_number + 1):
print '{}{}'.format('#' * (line_shape_number - step), '*' * step)