Creating a diamond pattern using loops

后端 未结 5 441
予麋鹿
予麋鹿 2021-01-03 17:17

I am trying to write a program that reads an integer and displays, using asterisks, a filled diamond of the given side length. For Example, if the side length is 4, the prog

5条回答
  •  灰色年华
    2021-01-03 17:39

    Thanks Guys, I was able to formulate/correct my code based on the help I received. Thanks for all your input and helping the SO community!

    if userInput > 0:              # Prevents the computation of negative numbers
        for i in range(userInput):
            for s in range (userInput - i) :    # s is equivalent to to spaces
                print(" ", end="")
            for j in range((i * 2) - 1):
                print("*", end="")
            print()
        for i in range(userInput, 0, -1):
            for s in range (userInput - i) :
                print(" ", end="")
            for j in range((i * 2) - 1):
                print("*", end="")
            print()
    

提交回复
热议问题