#๐Ÿ”’ Nested for loop

520 messages ยท Page 1 of 1 (latest)

silk monolith
final vineBOT
#

@silk monolith

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.

silk monolith
#

@naive ore

flint garnet
#

whats your goal?

normal pilot
#

Ok I see what you're trying to do

normal pilot
flint garnet
#

ooh

#

like visually?

normal pilot
#

see their output comments

silk monolith
#

oh ye

#

this is my goal

#

im stuck

#

at loops

naive ore
#

Do you have any code yet?

silk monolith
#

for like 3 months now

silk monolith
silk monolith
#

like i didnt even know where to start

#

started coding in like january

#

and i got stuck at loops

#

for 3 months

naive ore
#

what is the first loop you will need for this? it prints 5 lines of output, right?

silk monolith
#

i guess so

#

so set height = 5

#

for i in range(height):

naive ore
silk monolith
#

and now im stuck

#

idk what to do from here

naive ore
naive ore
silk monolith
#

so we are going down in a step of -1

#

so second for loop

#

for j in range(height-1)

naive ore
silk monolith
naive ore
silk monolith
naive ore
silk monolith
#

i know when to use it kinda

#

but i dont understand what it does

#

it just makes it so output isnt just

#

"********************"

naive ore
#

Notice that all of your lines look the same, why is that?

silk monolith
#

well i suppose it's meant to be like that, no?

silk monolith
#

im missing something like height-1

naive ore
flint garnet
silk monolith
#

and not clutter it

#

but my logic is hindered by doing so

naive ore
flint garnet
#

you could do it so it ends with ', ' so when you print values, it goes "a, b, c, d..."

naive ore
#

(i have to be away, others will help)

flint garnet
#

ill help dw

silk monolith
#

well theres nothing distinct in a line of code i have written so far

flint garnet
#

so

#

@silk monolith

silk monolith
flint garnet
#

what are you tying to do again?

#

print a tree right?

silk monolith
flint garnet
silk monolith
#

im tryna outpyt this

flint garnet
#

yea

#

so

#

first line

#

how much spaces does it have?

silk monolith
#

6-1

flint garnet
#

second?

silk monolith
#

wait no

#

it has 4

#

2nd line has 3

#

3rd line has 2

flint garnet
#

yeah

silk monolith
#

so there is a pttern of -1

flint garnet
#

yeah

#

-1 space, +1 *

#

the last row has 5 *

#

and 0 spaces

#

and the first one has 4 apces and 1*

#

each loop, you want one less space and one more star

silk monolith
#

ok im confused i just messed around with my code and it actually worked

#

i have no idea

#

how i did it

#

why i did it

flint garnet
#

wait

silk monolith
#

i just did trial and error

normal pilot
#

OK A solution has been found

silk monolith
#

this has taken me an entire day to solve

flint garnet
#

oof

ruby forge
#

Do you understand why the outer loop (for i)?

silk monolith
#

and i still dont understand how

silk monolith
#

i just did trial and error

ruby forge
#

Ok.

silk monolith
#

for a whole day

#

sounds like im a bad coder icl

flint garnet
#

nu uh

ruby forge
#

Think: how many rows do you want to draw? That is the height.

flint garnet
flint garnet
#

you just need to learn

silk monolith
#

there's someone in my grade (grade 10) whos a professional coder and even made his own programming language

normal pilot
#

Ok well let me try helping:
rename i to row_number
rename j to num_spaces
rename k to num_stars

It may help

silk monolith
#

he's just turned 16 btw

ruby forge
#

A for loop iterates over something. In Python range(n) gets you an iterable on n items (0,1,2,3,4) in your case.

silk monolith
#

pure sweat

#

compiles

silk monolith
#

his own language

ruby forge
#

Tehn inside the loop you just do whatever is needed for that single line.

flint garnet
#

you should get used to naming your varibles names instead of letters

#

so you understand better

silk monolith
#

i just see youtube

#

but its terribly explained

flint garnet
#

yeah

silk monolith
#

it just explain basic stuff

#

not logic

#

NO video explains logic

#

they're all useless

normal pilot
#

I personally recommend W3 Schools python course

silk monolith
#

im so confused

ruby forge
#

Here you're taking HW's suggestion of the pattern - more spaces according to the current height (i) and fewer asterisks.

normal pilot
#

Its free

flint garnet
flint garnet
normal pilot
silk monolith
#

it explains basic stuff

#

i wanna become a coder

#

who can do all kidns of stuff in python

flint garnet
normal pilot
#

Basics is how it starts up

flint garnet
#

i always thought it was explaining everything in python

silk monolith
flint garnet
ruby forge
#

One approach might be: what would you tell someone else about doing this, on paper with a pen, if you were instructing them. Not for loops or anything, stuff like "write down lines of space and asterisks, this many, how many etc". In English. Then translate.

flint garnet
#

not an actual course when you learn step by step

silk monolith
#

yes this is a majour issue with the exam board in doing at GCSE

#

it does pseudocode

#

but our teacher

normal pilot
silk monolith
#

is horrid

ruby forge
silk monolith
#

also in our exam we have easy questions

#

i can do them

normal pilot
silk monolith
#

i get like 6/7 in the problems

normal pilot
#

none of them knew python

silk monolith
#

and always lose a mark due to logic error

silk monolith
flint garnet
silk monolith
#

can't explain it that well

ruby forge
#

You explained it to HW.

flint garnet
#

so just use something you're familiar with to represent what you're trying to do

silk monolith
flint garnet
#

like ms paint

silk monolith
#

i am gonna use this approach

flint garnet
silk monolith
flint garnet
#

sure

#

pick whatever explains it better

silk monolith
#

can u give me an example

#

not example

#

like challenge

#

like rn i did the star one

flint garnet
#

ah

ruby forge
#

Print the even numbers up to 20.

silk monolith
#

can you give me another

silk monolith
flint garnet
#

yea good into to loops

ruby forge
#

Tell me what you would do, if you were doing it on paper.

flint garnet
#

i already know a mistake he might make

#

well not a mistake, just something he can improve

ruby forge
#

Let the OP have a go.

flint garnet
#

yea

ruby forge
#

We're not after "best", we're after "working".

normal pilot
silk monolith
#

it worked

normal pilot
#

That is one solution

ruby forge
#

Yay.

silk monolith
flint garnet
#

now use a while loop

ruby forge
#

So, you seems ok with that one.

flint garnet
#

if you know what it is

silk monolith
#

alright ill use while loop

silk monolith
flint garnet
#

lets say it was from 1 to 100

silk monolith
#

while num <= 20*

ruby forge
#

That's because you don't change num in the loop. Since it never changes, you never reach your bound of 20.

flint garnet
silk monolith
#

no

#

it would be too slow

ruby forge
#

You also want to set it to 0 (or 1) before the loop. It will have the 20 from the for loop earlier otherwise.

silk monolith
#

and inefficient

flint garnet
#

what if you just do the same as the for loop does?

#

just make a varible and make it so that it increases every loop

#

i mean ill give you a hint, you can do this with like 4 lines of code

#

without the if

silk monolith
flint garnet
#

return is more like a function thing

flint garnet
#

you want the answer?

silk monolith
#

ah crap

#

im stuck

#

this is not working properly

flint garnet
#

wdym?

ruby forge
#

Again: you're not changing i. So the condition of the while never changes.

flint garnet
#

oh right

silk monolith
#

"changing i"

#

?

flint garnet
#

unlike for loops, while doesnt increase the number

ruby forge
#

A for loop takes each item from the iterable (above, range(1,21)).
For a while loop you provide that mechanism.

flint garnet
#

set i as i+1

ruby forge
#

i = i + 1

flint garnet
#

or

i+=1
silk monolith
ruby forge
#

You also want to set i = 1 above the loop.

#

Because you've got i=20 from the for loop preceeding it, and you need to start over.

flint garnet
silk monolith
ruby forge
#

Good, setting i=1

flint garnet
#

so while i is less than or equal to 20

silk monolith
#

still it doesn't work

ruby forge
#

Now add code to the while loop to add 1 to i each time.

silk monolith
#

or i could just

flint garnet
#

hm?

silk monolith
#

it worked

ruby forge
#

Yay.

flint garnet
#

yup

#

nice

silk monolith
#

why do i need to set i = 1

ruby forge
#

Do you understand why it's better?

silk monolith
#

at start

silk monolith
#

ye less code

#

more efficient?

ruby forge
#

Because earlier you had that for-loop counting i from 1 through 20. So i was 20 after that.

#

i needs to start somewhere anyway. Setting it to 1 is that somewhere.

flint garnet
#

yknow you can remove the if

ruby forge
flint garnet
#

each even number is +2 the before one

silk monolith
#

im gonna be honest

#

for this while loop one

#

i used gpt

#

for help

#

i thought it would guide me

flint garnet
#

its okay

silk monolith
#

it just gives the answer

flint garnet
#

i use it all the time

silk monolith
#

its so tempting

#

i feel like a good coder has no need to use gpt

ruby forge
#

The problem there is twofold: 1: GPT just makes stuff up sometimes and 2: you haven't figured out the solution yourself.

#

So: what's different about the new, working code.

flint garnet
silk monolith
#

i +=1

ruby forge
#

Right. Why is that important?

silk monolith
#

i have no clue

ruby forge
#

Ok. What does it do?

silk monolith
#

so i counts up to 20

#

but why is it at the end

flint garnet
#

wait lemme explain

silk monolith
#

shouldnt that be a logic error

ruby forge
#

Right. If you don't count, i stays at 1 and the while loop never finishes.

#

It is a logic error, but Python doesn't know what you're trying to do. So it can't tell you what's wrong.

silk monolith
#

if i did this

#

it wouldnt work at all

ruby forge
#

Yes.

silk monolith
#

now if i shift i+=1 at the end

#

it works?

#

confusing logic

flint garnet
#

ooh

#

so basically

#

when theres an if,loop,function, yadda yadda

ruby forge
#

The problem isn't putting i+=1 at the end (though that is the natural place for it, just before the next loop). The probem is that being inside the if it doesn't happen if the if is false.

silk monolith
#

oh

ruby forge
#

So you want the print() inside the if, because that should/should-not happen dpeneidng on the value of i.

#

But the i+=1 should always happen. So it goes outside the if.

silk monolith
#

i have a question

#

how did u learn this logic

#

not logic

#

chain of reasoning

#

to solve a problem

#

i am confused as hell

ruby forge
#

I already thought this way when I learnt to programme.

silk monolith
#

my thinking process is fixed

ruby forge
#

Basicly: you're telling Python exaclty what needs to be done. Leave nothing out.

silk monolith
#

alright can you give me another problem

#

that i can do

flint garnet
#

uhh

silk monolith
#

this one i kinda get now

flint garnet
#

do you know random?

silk monolith
#

yes

#

import random

flint garnet
#

use randit

silk monolith
#

i've used randit

flint garnet
#

and make a little game

silk monolith
#

trying to make number guessing game

ruby forge
#

TO me there are 2 basdic sides to this:

  • take your problem, express it in english as to a person.
  • Python needs to be told everything it needs to do. Translate your english into python, getting more detailed until everything has been said.
silk monolith
#

is there any job

#

computer science related

#

high paying

#

without coding

#

LMAO

#

sql is way easier than any coding language

#

im learnign that steadily as well

flint garnet
# flint garnet and make a little game

where the program picks a random number, and you try to guess it, it tells you if its higher or lower, the game ends when you correctly guess the random number

ruby forge
#

Oh wait until you write a complicated bit of SQL.

silk monolith
#

oh...

ruby forge
#

SQL's hard enough that we (programmers) often use ORMs (object relation managers) just to avoid writing it ๐Ÿ™‚

flint garnet
#

@silk monolith

silk monolith
flint garnet
#

make a number guessing game

#

BUT

#

first

#

tell us what your plan is

#

how will you make it

silk monolith
#

alright im writing it in # vscode first

#

then ill screenshot it here

flint garnet
#

nope

#

tell us

ruby forge
#

Describe it in english to us first.

flint garnet
#

explain what you're gonna do

#

@silk monolith

silk monolith
#

one question

#

when should i do

#

when should i use

#

a chain of if statemnts

#

opposed to a chain of if then elif elif elif

flint garnet
#

tell us what your plan is

#

then write the syntax

silk monolith
#

@flint garnet

flint garnet
#

hm

#

a long range would be better

#

like 1-100

#

also tell the player if their number is higher or lower

silk monolith
#

i've almost done it

#

btw

#

how do i use \n

#

new line

#

omg i did it

#

i think

#

@flint garnet

#

@ruby forge

flint garnet
#

chatgpt?

silk monolith
#

no i swear on allah

#

i didnt use gpt

ruby forge
#

Good.

#

But I suspect you tested it with a correct guess?

silk monolith
#

i did make some errors initially

#

but then

#

i tested it

#

and fixed it

ruby forge
#

I'm saying that because you pick a different random number on every pass through the loop, making the guess something of a moving target.

silk monolith
#

like i didnt have while loop initially

ruby forge
#

To see what's going on, add:

print("randomGuess", randomGuess)
#

after the randomGuess = line.

silk monolith
#

OHH

#

i see

ruby forge
#

Just do it once, before the loop.

silk monolith
#

i intergrated it within the loop

#

can you give me a problem where i have to use some looping

#

together

#

btw

#

when would you typically use for loops

#

iterating through lists

#

?

#

and what else

#

@naive ore @ruby forge @flint garnet thanks for the advice you're giving me btw helped me solve problems a bit better i guess

flint garnet
#

nice

silk monolith
flint garnet
#

i have an idea

#

tho idk if youll be able to do it

silk monolith
#

just tell me

flint garnet
#

math quiz

#

basically

silk monolith
#

what is 12 * 1

#

12 x 1

flint garnet
#

kinda

#

so

naive ore
#

another idea: print a multiplication table

flint garnet
#

hm

silk monolith
flint garnet
#

yeah good idea

silk monolith
#

like?

naive ore
#

(as a table with columns and rows if you get fancy)

silk monolith
#

like this?

flint garnet
#

yup

naive ore
silk monolith
silk monolith
#

there's a guy in my grade

#

who's learnt like 8 languages

#

made his own language

#

and compiler for it

#

and he codes for "fun"

naive ore
silk monolith
#

academic scholar as well

silk monolith
#

i kinda do that to a lot tho

#

like this year i had some pretty good grades

#

even got an A in comp sci

naive ore
silk monolith
#

you sure?

naive ore
silk monolith
#

im stuck

flint garnet
#

okay

#

explain what you're thinking

silk monolith
#

well you have rows

#

and then columns

#

lets set both to 12

#

so we have 2 nested for loops

naive ore
#

yes

flint garnet
#

have you used loops in loops?

silk monolith
silk monolith
flint garnet
#

cus

#

range doesnt supposrt end

silk monolith
#

oh

flint garnet
#

end is for strings

silk monolith
#

ok i know now

flint garnet
#

range(start, stop, step)

silk monolith
#

im thinking this so far

#

but its not giving me the cofrrect thing

flint garnet
#

if theres only one value then its stop by deafault

#

copy the code here

final vineBOT
#

Hey @silk monolith!

Please edit your message to use a code block

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

This will result in the following:

print('Hello, world!')```
flint garnet
#

ah

silk monolith
#
for column in range(13):
    for row in range(13):
        print(column, end = " ")
        print(row)
flint garnet
#

its correct

silk monolith
flint garnet
#

the formatting isnt tho

silk monolith
flint garnet
#

yeah

#

yknow why?

silk monolith
#

its due to my for loop

#

it iterates in a wrong way

flint garnet
#

what does end="" do?

silk monolith
#

not wrong by python

silk monolith
#

?

flint garnet
#

nope

#

the name tells you

silk monolith
#

ends it

#

on that line

flint garnet
#

with what?

silk monolith
#

a "

#

""

flint garnet
#

yup

#

anything you put in thoes, itll end the string with it

silk monolith
#
, end = " "
#

so remove this?

flint garnet
#

what would

print("test", end=", ")
print("try")

do?

#

what would it print?

silk monolith
#

test, try

flint garnet
#

yup

#

now split that into what prints what

silk monolith
#

well it's same as

#
print("test,try")
#

columb, row

#

12, 1

#

12, 2

#

12, 3

#

thats what i was getting

#

what do i do here

flint garnet
silk monolith
#

by this question

flint garnet
#

you have "test, try"

silk monolith
#

ye

flint garnet
#

one part is the first print, another part is the end to the first print, and the last part if from the second print

#

so where are the parts split

#

?

silk monolith
#

i see what im doing wrong

#

im not actually doing any multiplication

#

``py

#
for i in range(1, 7):
    for j in range(1, 7):
        print(i * j, end=" ")
    print()
final vineBOT
#

Hey @silk monolith!

Please edit your message to use a code block

Make sure you put your code on a new line following py. There must not be any spaces after py.

Here is an example of how it should look:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
final vineBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.