Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 程序,创建三角形和正方形_Python_Shapes - Fatal编程技术网

Python 程序,创建三角形和正方形

Python 程序,创建三角形和正方形,python,shapes,Python,Shapes,我已经开始学习编程,我需要创建一个程序,用户可以输入所需的行数,然后程序必须根据用户提供的信息打印两种不同的形状。形状必须像 大宗报价 我设法做了三角形,但我不知道如何创建内部为空的正方形。我只在里面填过。 有人能帮我修改代码吗 userInput = input("Enter amount of row's wanted: ") def shape(userInput, drawCharacter): n = 0 while n < int(userInput):

我已经开始学习编程,我需要创建一个程序,用户可以输入所需的行数,然后程序必须根据用户提供的信息打印两种不同的形状。形状必须像

大宗报价

我设法做了三角形,但我不知道如何创建内部为空的正方形。我只在里面填过。 有人能帮我修改代码吗

    userInput = input("Enter amount of row's wanted: ")
def shape(userInput, drawCharacter):
    n = 0
    while n < int(userInput):
        n += 1
        if drawCharacter == "*":
            print(n*drawCharacter.rjust(3))
        elif drawCharacter == "#":
            print(int(userInput)*drawCharacter.rjust(3))

shape(userInput, "*")

print("|__________________|\n")

shape(userInput, "#")
userInput=input(“输入所需行的数量:”)
定义形状(用户输入,绘图字符):
n=0
当n
您的盒子基本上由以下部分组成:

  • 顶行和底行:
    打印(宽度*'#')
  • 和中间行:
    print('.{}.'.format('.*(宽度-2))

  • 作为练习,你只需要找出循环

    在生成矩阵时使用
    numpy
    数组避免循环的方法:

    import numpy
    
    n = 5 # or userinput, has to be >= 2
    mat = np.full((n,n), '#') # a matrix of #s
    mat[1:-1, 1:-1] = np.full((n-2, n-2), ' ') # make the center of the matrix ' '
    print('\n'.join([' '.join(e) for e in mat]))
    
    结果:

    # # # # #
    #       #
    #       #
    #       #
    # # # # #
    

    如果这是您第一次接触编程(任何语言),我建议您尝试使用嵌套for循环来实现此问题(这将模拟二维数组,或者基本上是矩阵),尝试找出不打印的矩阵索引es,并仅打印边缘。
    通过使用这种方法,您将更好地深入了解此任务所呈现的问题以及如何解决它。祝你好运

    我建议你离开电脑。拿一支笔和一张纸,用文字写下创建正方形的步骤。寻找它是如何构建的模式。基本的构造块是什么?嗨,Jung Carl,欢迎来到stackoverflow。我可以看出你正在努力解决一个初学者(家庭作业?)的问题,我们不只是想为你做这项工作,因为。。。那将挫败学习的乐趣。我将给你一个提示:你可以设计什么样的测试来回答“这是第一篇还是最后一篇专栏?”的问题,并看看这会把你带到哪里。祝你好运我投票结束这个问题,因为它似乎是一个家庭作业问题
    # # # # #
    #       #
    #       #
    #       #
    # # # # #