[Python Code] Python in hình kim cương

Python in một hình kim cương các ký tự.

def draw_diamond(n):
    if n % 2 != 0:
        k = 1
        while k <= n:
            print(" " * int((n - k) / 2) + "*" * k + " " * int((n - k) / 2))
            k += 2

        j = 1
        while (n - 2 * j) >= 1:
            print(" " * j + "*" * (n - 2 * j) + " " * (j))
            j += 1
    else:
        print("Invlidate.")

def main():
    n = int(input("Nhâp một số lẻ = "))
    draw_diamond(n)
    return 0


if __name__ == "__main__":
    main()

 

Input: 15

Ouput:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.