如何在python中绘制没有龟的东西

时间:2017-01-27 15:17:41

标签: python

我试图用星号画一个正方形,但这段代码不起作用 这是代码

def drawStar(numStars):
    for x in range(0,numStars):
        print("* ")

def menu():
    # prompting the user to pick what they want drawn
    input("Welcome to my draw shapes program ")
    print("What would you like me to draw")
    input(" Draw a Square (1)\n Draw a Rectangle (2)\n Draw a Rectangle (2)\n Draw an Arrow Head (3)\n Exit (4)")

def drawSquare():
   width = int(input("What is the width of your square "))
   for x in range(0,width):
        drawStar(width)
drawSquare()

这是我一直得到的输出

What is the width of your square 2
* 
* 
None
* 
* 
None

2 个答案:

答案 0 :(得分:0)

矩形:

m, n = 10, 10
for i in range(m):
    for j in range(n):
        print('*' if i in [0, n-1] or j in [0, m-1] else ' ', end='')
    print()

三角:

m, n = 10, 10
for i in range(m):
    for j in range(n):
        print('*' if i in [j, m-1] or j == 0 else ' ', end='')
    print()

答案 1 :(得分:0)

print("* ")

默认情况下,print()函数会在您提供的数据后打印换行符。这就是为什么你在一个单独的行上看到每个星号(和空格)的原因。 print()函数接受名为end的关键字参数,以指定在末尾打印的其他内容。

你可能想要这样的东西:

def drawStar(numStars):
    for x in range(0,numStars):
        print("* ", end='')
    print()

或者,您可以使用python的功能,即字符串可以乘以数字:

def drawStar(numStars):
    print( "* " * numStars )