#๐Ÿ”’ help

105 messages ยท Page 1 of 1 (latest)

twin dew
#

how to do this with nested loops

trail caveBOT
#

@twin dew

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.

twin dew
#

i got the first 3 lines but not the bottom 2 lines

sinful gyro
#

What's the code you have so far?

twin dew
#

for i in range(0,3):
for j in range(i,3):
print("#",end="")
for k in range(0,2*i+1):
print("n",end="")
print()

trail caveBOT
#

Hey @twin dew!

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.
twin dew
#

only the top 3 lines

sinful gyro
#

!e

for i in range(0,3):
    for j in range(i,3):
        print("#",end="")
    for k in range(0,2*i+1):
        print("n",end="")
    print()
trail caveBOT
twin dew
#
for i in range(0,3):
    for j in range(i,3):
        print("#",end="")
    for k in range(0,2*i+1):
        print("n",end="")
    print()
#

the # is space

#

just for easier understanding

#
for i in range(0,3):
    for j in range(i,3):
        print(" ",end="")
    for k in range(0,2*i+1):
        print("n",end="")
    print()
#

!e

trail caveBOT
#
Missing required argument

code

full prism
#

what are you struggling with?

twin dew
#

the bottom 2 lines

full prism
#

do you know about the range() function? do you know what parameters it can take?

twin dew
#

i dont know how to make the loop for the last 2 lines

full prism
#

!d range

trail caveBOT
#

class range(stop)``````py

class range(start, stop[, step])```
The arguments to the range constructor must be integers (either built\-in [`int`](https://docs.python.org/3/library/functions.html#int) or any object that implements the [`__index__()`](https://docs.python.org/3/reference/datamodel.html#object.__index__) special method). If the *step* argument is omitted, it defaults to `1`. If the *start* argument is omitted, it defaults to `0`. If *step* is zero, [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError) is raised.

For a positive *step*, the contents of a range `r` are determined by the formula `r[i] = start + step*i` where `i >= 0` and `r[i] < stop`.

For a negative *step*, the contents of the range are still determined by the formula `r[i] = start + step*i`, but the constraints are `i >= 0` and `r[i] > stop`.
full prism
#

are you familiar with the step parameter?

twin dew
#

nope

#

like

#

how to lesten the range

#

lessen

full prism
#

yeah, imagine you have a range from 1 to 10

#

but you want only odd numbers

#

so you would go every second number, skipping one

#

so you would have a step of 2

#

you would step 2 numbers at once

#

!e

print(list(range(1,10,2)))
trail caveBOT
full prism
#

the first 2 parameters are start and stop

#

if stop is greater than start, then the step parameter can be negative

#

!e

print(list(range(10,1,-2)))
trail caveBOT
full prism
#

so you can go the reverse order

#

do you understand?

#

for the next 2 lines, you just need a reverse for loop from the initial one

twin dew
#

one second im reading

#

ohh

#

thank you

full prism
#

no problem, do you have any other questions?

twin dew
#

yeah

full prism
#

tell me

twin dew
#

hold on

#

i havent even given this one a thought

#

how the heck do u even do this

full prism
#

I would do it in a tricky and hacky way

#

like write the output till 1, and store it as a string, and then reverse the string, remove the one, add both parts together and print it

twin dew
#

but

#

i have to do this only with nested loops

#

its achapter

full prism
#

have you learnt about end parameter of the print statement?

#

!d print

trail caveBOT
#

print(*objects, sep=' ', end='\n', file=None, flush=False)```
Print *objects* to the text stream *file*, separated by *sep* and followed by *end*. *sep*, *end*, *file*, and *flush*, if present, must be given as keyword arguments.

All non\-keyword arguments are converted to strings like [`str()`](https://docs.python.org/3/library/stdtypes.html#str) does and written to the stream, separated by *sep* and followed by *end*. Both *sep* and *end* must be strings; they can also be `None`, which means to use the default values. If no *objects* are given, [`print()`](https://docs.python.org/3/library/functions.html#print) will just write *end*.

The *file* argument must be an object with a `write(string)` method; if it is not present or `None`, [`sys.stdout`](https://docs.python.org/3/library/sys.html#sys.stdout) will be used. Since printed arguments are converted to text strings, [`print()`](https://docs.python.org/3/library/functions.html#print) cannot be used with binary mode file objects. For these, use `file.write(...)` instead.
twin dew
#

wait

#

yes

#

end=''

full prism
#

yeah

twin dew
#

what about it

full prism
#

you can generate the first part with a for loop, print it, and the generate the other part (the other half of the row) with another for loop

#

and using the end="" parameter, will print the characters on the same row

twin dew
#

wait

#

the 1st one still isnt working

full prism
#

send the code as plain text

full prism
# twin dew <@481070779203584000> can u call?

I am quite an introvert for that, and I usually find myself explaining better while writing, cuz I have more time to think and I sometimes make myself not understood because I am not a great speaker

twin dew
#
for i in range(0,3):
    for j in range(i,3):
        print(" ",end="")
    for k in range(0,2*i+1):
        print("n",end="")
    print()
for x in range(2,0,-1):
    for y in range(x):
        print(" ",end="")
    for z in range(1,x):
        print("n",end="")
    print()
twin dew
#

im an introvert too but its online

#

so i dont really care

#

but all good your wish

sinful gyro
# twin dew

It seems like there aren't enough spaces, so I would increase the range for for x in range. 2 might be too low

twin dew
#

my brain isnt braining

#

this shit is fucking my brain up

twin dew
#

ive tried everything

ebon hawk
#

you're gonna print identical lines again. literally copy paste it ?

#

only difference is you do the lines in reverse order and don't repeat the middle

twin dew
#

this is what im trying to make

twin dew
ebon hawk
#

just copy paste the same code and reverse the loop

twin dew
#

how do i reverse the loop

ebon hawk
#

if you can count from 1 to 3 then surely you can count from 3 to 1

#

(2 to 1, since not including the middle)

twin dew
#

bruh

#

what the hell

#

i literally cant

ebon hawk
#
i = 3
while i > 0:
    i -= 1
#

I'm not saying that's what you should write, but I am saying that you should have no trouble doing it THAT way even if for some reason doing it with a for-loop is giving you trouble

#

ie. start at 3, and repeatedly subtract 1

twin dew
#

but i have to do it with only for loop

#

my test is on nested loops

ebon hawk
#

also: you've already posted code that has a for-loop that counts down

#

though for some reason you changed the loop body in it even though the lines are identical and thus would use the same code as the first 3 lines

keen cape
# twin dew how to do this with nested loops
rows = 4
for i in range(rows):
    for j in range(rows - i - 1):
        print(" ", end="")
    for k in range(2 * i + 1):
        print("*", end="")
    print()

for i in range(rows - 1):
    for j in range(i + 1):
        print(" ", end="")
    for k in range(2 * (rows - i - 1) - 1):
        print("*", end="")
    print()
trail caveBOT
#
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.

#

๐Ÿ”’ help