Print a Perfect ‘X’ in Python in Seconds! | Easy Coding Trick#coding #programming #codewithdeveloper

preview_player
Показать описание
Want to create a cool ASCII ‘X’ pattern in Python with just a few lines of code? In this quick tutorial, you’ll learn how to print a perfect ‘X’ inside an n x n grid. It’s a fun, beginner-friendly coding challenge that will help sharpen your programming skills. Try it out with different sizes and watch your ASCII art come to life! Make sure to like, share, and subscribe for more Python tips and tricks!

#Python #Coding #Programming #ASCIIArt #LearnPython #CodeNewbie #Developers #Shorts #TechTutorial
Рекомендации по теме
Комментарии
Автор

n = int(input("X size: "))
h = int(n / 2)

for i in range(h):
print(" " * i + "\\" + " " * (n - i * 2 - 2) + "/")

if n % 2 == 1:
print(" " * h + "X")

for i in range(h):
print(" " * (h - i - 1) + "/" + " " * (i * 2 + n % 2) + "\\")

rainbowskeppy
Автор

Why not just do 1/4 of the grid and replace the chars to reflect it? Just make sure to handle the middle with a separate conditional. Also if you preload all spaces and just change the index where array[n][n] you only need one for loop.

mberlinger
Автор

Simple solution

n = int(input("Enter n: "))

# st will point at the first element
# en will point at the last element
st = 0
en = n - 1

for _ in range(n):
for j in range(n):
# st and en will never be equal unless the size is odd
if (st == en == j):
print("X", end="")
elif (j == st):
print("\\", end="")
elif (j == en):
print("/", end="")
else:
print(" ", end="")

# go to the next line
print()
# make st go to the next element (exmpl: 0 -> 1)
st+= 1
# make en go to previous element (exml: 6 -> 5)
en -= 1

HoussemDegachi
Автор

`grid = lambda n: [['\\' if i==j else '/' if i==n-j-1 else ' ' for j in range (n)] for i in range(n)]`


and for printing

`printgrid= lambda n: print('\n'.join([' '.join(i) for i in grid(n)]))`

taektiek
Автор

The length of these is longer than N, it's sqrt(N^2+N^2) long.

Burbun
Автор

If both initial conditions match you print an X

sjfreitas
Автор

What language was the problem description diagram drawn with ?

KanjiCoder_RTFM