Draw a square #python #coding #softwareengineer

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Dont worry guys, it’s actually just a one liner:

def square(n):
print((n * “#” + “\n”) + ((n-2) * (“#” + ((n-2) * “ “) + “#\n”)) + (n * “#”))

philstanton
Автор

Please keep making the video from basics to advance. It was very much interesting the way you teach. Thank you so much ❤

groot
Автор

You could also do the top and then the bottom portion outside of the for loop, as to avoid the if statement all together.

agentotten
Автор

order = int(input("Insira um valor natural: "))
for n in range(order):
print('x ' * order if n == 0 or n == order-1 else 'x ' + ' ' * (order-2) + 'x')

arielsouza
Автор

The problem with these tutorials or showcases of these problems on YouTube is that the person has probably seen it before many times. So they'll always has the perfect answer

dot
Автор

def print_square(n: int):
print('#' * n)
for i in range(n - 2):
print(f"#{' ' * (n - 2)}#")
print('#' * n)

krishnanandjb
Автор

move edge condition out of loop to upgrade your coding level to intermediate.

DatNickNganGonVaDeNho
Автор

I feel ur pain switching up the syntax ive done that too many times😅

Hacky
Автор

Nice man! I did not know that we can do this in that way in python

shaqib
Автор

You can give it list of points and just check if the point is in a list if it does add X

shalevforfor
Автор

def DrawSquare(n:int)->str:
Square =[]
for i in range(n):
if i==0 or i==n-1:
Square.append('x '*n + '\n')
else:
Square.append('x ' + ' '*(n - 2) +'x '+'\n')
return ''.join(Square)

print(DrawSquare(10))

DeseLoki-kmsu
Автор

Not able to get this else statement plz define this... Plz reply

aditichamsan
Автор

def print_square(n):
a = " ".join("X" * n)
b = "".join(a.split())
c = " ".join(f"{b[0]}{' ' * (n -2)}{b[0]}")
print(a)
[print(c) for each in range(0, n -2)]
print(a)

print_square(5)

jmac
Автор

You're all ignoring the cleanest solution:
for x in range(n):
for y in range(0, n):
if x == n - 1 or y == 0 or y == n - 1 or not x:
print('X', end = "")
else:
print(" ", end = '')
print('')

dIancaster
Автор

if ((i % (n - 1) == 0) || (j % (n - 1) == 0)) that will be enough except n=1

apollonir
Автор

That was one of the easiest one I have done.

josuetoledo
Автор

Writing the least understandable code speedrun

bryceblazegamingyt
Автор

Too many people defaut to if else when ternary exists

darkmift
Автор

Here's the best way to do this, imo

def print_rectangle(n):
print("X X X X X")
for each in range(0, n):
print("X X")
print("X X X X X")

print_rectangle(5)

Germisstucklmao
Автор

# © Pawlo370
def kwadrat(x):
for i in range(x):
if i%(x-1) == 0:
print("X "*x)
else:
print("X "+" "*(x-2)+"X")

kwadrat(5)

Pawlo