#πŸ”’ Understanding issue

204 messages Β· Page 1 of 1 (latest)

carmine dove
#

I spent 3 days to learn the logic behind this code and its star pattern I'm still unable to understand it.

pale sailBOT
#

@carmine dove

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

jolly ember
turbid anchor
#

!code

pale sailBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

carmine dove
carmine dove
#

n = int(input("Enter the number :"))
for i in range (1, n+1):
print(" "* (n-i), end="")
print("" (2*i-1), end="")
print("")

pale sailBOT
#

Hey @carmine dove!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
turbid anchor
#

Is there anything here you do understand?

carmine dove
#

nope

jolly ember
carmine dove
#

yes

jolly ember
carmine dove
#

yes

jolly ember
#

So you do understand something

turbid anchor
#

do you understand what a for loop does?

jolly ember
#

do you understand how to for loop?

carmine dove
#

yes but

carmine dove
carmine dove
#

not much

#

but yea

turbid anchor
#

ok so what part of it don't you understand?

carmine dove
#

the part after first line

jolly ember
#

do you understand what range() is?

carmine dove
turbid anchor
#

it doesn't print them

jolly ember
turbid anchor
#

it simply creates a sequence of numbers

carmine dove
#

sequence, ok.

turbid anchor
#

range(5) is basically the same as 0, 1, 2, 3, 4

carmine dove
#

yes

#

ik this

#

if i write range(0,5,2) : it will give sequence from 0 to 5 with 2 number space

#

ik range

#

yes

turbid anchor
#

ok so we still don't know what you don't know then

carmine dove
#

sorry for saying idk but ye i remember now

carmine dove
turbid anchor
#

code works by running each line

carmine dove
#

like n = int(input("Enter the number :"))
for i in range (1, n+1):
print(" "
(n-i), end="")
print("" (2*i-1), end="")
print("")
*

jolly ember
carmine dove
#

This line

turbid anchor
#

that's not a line, that's the entire code

carmine dove
carmine dove
jolly ember
#

yes its a variable and it holds the value from the iterable list range()

jolly ember
#

And after each iteration, it goes to the next value

jolly ember
turbid anchor
#

!e

for i in range(5):
    print(i)
pale sailBOT
carmine dove
#

but idk what's goin on in the code, man

turbid anchor
#

but you understand each component of it?

jolly ember
carmine dove
carmine dove
turbid anchor
#

do you understand that you can multiply strings?

#

!e

for i in range(1, 6):
    print('*' * i)
pale sailBOT
jolly ember
#

!e

print("hello"*3)
pale sailBOT
carmine dove
#

bruh

#

n = int(input("Enter the number :"))
for i in range (1, n+1):
print(" "
(n-i), end="")
print("" (2*i-1), end="")
print("")
*

jolly ember
carmine dove
#

just explain me bcz the numbers and variables

#

are confusing

turbid anchor
#

it prints a specific number of spaces, followed by a specific number of stars

#

the amount is determined by i

#

!e

n = 5
for i in range(1, n+1):
    print(f"i is {i} ", end='')
    print(" " * (n-i), end="")
    print("*" * (2*i-1), end="")
    print("")
pale sailBOT
carmine dove
#

ik the output bro

turbid anchor
#

haha yeah, had a typo

#

should be n-i

#

!e

n = 5
for i in range(1, n+1):
    print(f"i is {i}, {n-i=} spaces, {2*i-1=} stars  ", end='')
    print(" " * (n-i), end="")
    print("*" * (2*i-1), end="")
    print("")
pale sailBOT
carmine dove
#

i started to understand

turbid anchor
#

!e

n = 5
for i in range(1, n+1):
    print(f"i is {i}, {n-i=} spaces, {2*i-1=} stars  ", end='')
    print("." * (n-i), end="")
    print("*" * (2*i-1), end="")
    print("")
pale sailBOT
carmine dove
#

but the main thing is that how would i write a code if i were to draw a different pattern

turbid anchor
#

instead of spaces, here is dots

#

so you can better understand

turbid anchor
#

this isn't universal "draw a pattern" code. Every pattern will be different

carmine dove
#

e

turbid anchor
#

show us what that would look like

#

it's a lot easier to write code to create a shape if you've already created that shape

#

then you can work backwards

carmine dove
#

like this

turbid anchor
#

I already did that exact one above

carmine dove
turbid anchor
#

!e

print("*" * 1)
print("*" * 2)
print("*" * 3)
print("*" * 4)
print("*" * 5)
pale sailBOT
turbid anchor
#

it's the same as this, we're just using i and range to make it simpler

carmine dove
#

tysm dude

#

@jolly ember @turbid anchor Thanks alot

turbid anchor
#

I'd really recommend reviewing some of the basics of python

#

!res

pale sailBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

turbid anchor
#

we have some great resources here you can look through

carmine dove
#

but

#

n = 5
for i in range(n): # OUTTER LOOP FOR ROWS

for j in range(i+1): # INNER LOOP FOR COLOUMNS
    print(" ", end=' ')
for j in range(i,n):
    print("*", end=' ')
    
print() #VERY IMPORTANT
#

***py
n = 5
for i in range(n):# OUTTER LOOP FOR ROWS

for j in range(i+1):# INNER LOOP FOR COLOUMNS
    print(" ", end=' ')
for j in range(i,n):
    print("*", end=' ')
    
print()#VERY IMPORTANT

jolly ember
carmine dove
#

ok

carmine dove
#

n = int(input("Enter the number :"))
for i in range (1, n+1):
print(" "
(n-i), end="")
print("" (2*i-1), end="")
print("")
*
@jolly ember can u tell me explanation from this line?

#

the one is bold

jolly ember
carmine dove
#

can i explain to you and check if im wrong

jolly ember
#

pls do

carmine dove
#

ok so

#

welcome

#

so in first line

#

you gotta put the input

#

and make sure u got int

#

bcz without int the interpreter will think you are giving a string

#

so

#

in 2nd line

#

you print space

#

and (n-i), end=" " means that n is the input and it will minus 1 from the input like in decreasing order

#

and the "end=" fuction doesnt print new line so it helps in making these patterns

#

and 3rd line

#

we printed a space

jolly ember
carmine dove
#

mb

#

ok so

#

after that

#

we did (2*i -1 ) which means, if you see a triangular pattern it has stars in odd numbers

#

like 1 , 3, 5

#

thats how it goes

#

so if we multiply 2 to the i which is the number sequence it will give us the output of the odd numbers

#

which we need for the construction of triangle

#

and same function we do ( end = "" )

#

and then

#

in the last line u see print with indentation

#

indentation is quite important

#

so print( " " ) means that

jolly ember
carmine dove
#

wait

#

if we didnt print the print(" " ) in the end it would give us the new line

#

we needed a new line

#

we could've put print("\n")

#

but it would've give us double line because \n has its own line and print function also gives an extra line

#

but we need a single line space

#

so we

#

only printed and empty print

#

das it

#

*** THE END ***

#

watchu think

jolly ember
#

nearly perfect

carmine dove
jolly ember
carmine dove
#

for example

#

look at this

jolly ember
#

you can still get a triangle with running numbers

carmine dove
#

it starts from 1 then 2 then 3 then 4 and its not a perfect triangle

carmine dove
#

we need this

jolly ember
#

maybe you need to use proper terms.

carmine dove
jolly ember
jolly ember
carmine dove
jolly ember
carmine dove
#

btw agent

#

for how long ur doiin pytho

#

python

jolly ember
#

I can't remember

carmine dove
#

dayum

#

is that flex or wha

jolly ember
#

Nah. Gettin old bro

carmine dove
#

fr?

#

if u dont mind would ya tell me?

jolly ember
#

tell ya wat?

carmine dove
#

like how old you got

jolly ember
#

35

carmine dove
#

bro what

#

35 dude

#

respec fr

#

didnt know i was talking to a 35 year old

#

thanks for the help sir

jolly ember
#

np

#

Type !close when you're done πŸ™‚

carmine dove
#

oh yeah i forgot

#

!close

pale sailBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.