我不知道';I don’我不知道如何在python中使用#';s";及*';s";
我正在尝试创建一个简单的python代码,该代码将使用“#”和“*”创建相当简单的形状,我对python非常陌生,只想使用我不知道';I don’我不知道如何在python中使用#';s";及*';s";,python,python-3.x,shapes,Python,Python 3.x,Shapes,我正在尝试创建一个简单的python代码,该代码将使用“#”和“*”创建相当简单的形状,我对python非常陌生,只想使用for循环和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
for循环
和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#形状总数
对于步进范围(1,线条形状编号+1):
打印“{}{}”。格式(“#”*(线条形状编号-步骤),“*”*步骤)
我尝试了一些东西,但忘了检查并删除它。非常感谢这对我很有帮助。是的,很抱歉也是Stackoverflow的新手。告诉你,除非你得到解决方案或它有助于解决问题,否则请接受答案。如果有多个答案,比较并接受它们。由你决定。请访问部分了解更多详细信息。如果print(“#”*(n-i-1)+“*”*i)
看起来非常有效且更简单,有人能解释一下为什么要使用字符串格式吗?