#๐ Nested for loop
520 messages ยท Page 1 of 1 (latest)
@silk monolith
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.
@naive ore
whats your goal?
Ok I see what you're trying to do
Make a tree of stars
see their output comments
Do you have any code yet?
for like 3 months now
ye let me show u what i tried
actually i dont have any for this problem it confused me alot
like i didnt even know where to start
started coding in like january
and i got stuck at loops
for 3 months
i see you are frustrated. You don't need to tell us you are stuck at loops again.
what is the first loop you will need for this? it prints 5 lines of output, right?
ye
i guess so
so set height = 5
for i in range(height):
ok, good start.
for the first line, how many spaces do you need to print, and how many stars?
well it is 4 spaces
yes, for the first line, and how many spaces for the second line?
3
so we are going down in a step of -1
so second for loop
for j in range(height-1)
make a program with those two loops, and print the values of i and j, and see if they are what you want.
but first, pick better names. what does i represent, and what does j?
you did very well to use end="", was that your own idea?
ye it's in a text book of mine to use it
i know when to use it kinda
but i dont understand what it does
it just makes it so output isnt just
"********************"
it means, print stuff, and don't end the line yet.
Notice that all of your lines look the same, why is that?
well i suppose it's meant to be like that, no?
"it's meant to be like that"? It's your program you decide what it should be like.
end() basically tells the code what comes after you text
i guess im trying to make it readable
and not clutter it
but my logic is hindered by doing so
sorry, when I said your lines are all the same I meant the lines of output
you could do it so it ends with ', ' so when you print values, it goes "a, b, c, d..."
(i have to be away, others will help)
oh
ill help dw
well theres nothing distinct in a line of code i have written so far
this?
6-1
second?
yeah
so there is a pttern of -1
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
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
wait
i just did trial and error
OK A solution has been found
this has taken me an entire day to solve
oof
Do you understand why the outer loop (for i)?
and i still dont understand how
i have no idea at all man
i just did trial and error
Ok.
nu uh
Think: how many rows do you want to draw? That is the height.
everyone can code
you just need to learn
there's someone in my grade (grade 10) whos a professional coder and even made his own programming language
Ok well let me try helping:
rename i to row_number
rename j to num_spaces
rename k to num_stars
It may help
he's just turned 16 btw
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.
damn
nice
mhm i see
dubs
yea good tip
his own language
Tehn inside the loop you just do whatever is needed for that single line.
you should get used to naming your varibles names instead of letters
so you understand better
oh
i just see youtube
but its terribly explained
yeah
it just explain basic stuff
not logic
NO video explains logic
they're all useless
I personally recommend W3 Schools python course
im so confused
Here you're taking HW's suggestion of the pattern - more spaces according to the current height (i) and fewer asterisks.
Its free
they have a course?
a free one too?
i've used it
it explains basic stuff
i wanna become a coder
who can do all kidns of stuff in python
huh
Basics is how it starts up
i always thought it was explaining everything in python
well i got the basics in the bag i think
and other stuff
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.
not an actual course when you learn step by step
Welcome to Pseudocode
yes this is a majour issue with the exam board in doing at GCSE
it does pseudocode
but our teacher
they try... a lil
is horrid
Aye, but a good way from going from your mental idea of what to do to actionable things.
I feel ya, i had 3 teachers in one year
i get like 6/7 in the problems
none of them knew python
and always lose a mark due to logic error
nah he knows python
yeah, its like learning a new language, to speak, you need to know the translations, then you get used to knowing what the new words mean
can't explain it that well
You explained it to HW.
so just use something you're familiar with to represent what you're trying to do
alright can you give me an example that i code now
like ms paint
i am gonna use this approach
ill use pen and paper
can u give me an example
not example
like challenge
like rn i did the star one
ah
Print the even numbers up to 20.
can you give me another
alright
yea good into to loops
Tell me what you would do, if you were doing it on paper.
i already know a mistake he might make
well not a mistake, just something he can improve
Let the OP have a go.
yea
We're not after "best", we're after "working".
As always
That is one solution
Yay.
So, you seems ok with that one.
if you know what it is
alright ill use while loop
lets say it was from 1 to 100
while num <= 20*
That's because you don't change num in the loop. Since it never changes, you never reach your bound of 20.
would you write each number 1 to 100?
You also want to set it to 0 (or 1) before the loop. It will have the 20 from the for loop earlier otherwise.
and inefficient
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
aight
when do i use return
return is more like a function thing
wdym?
Again: you're not changing i. So the condition of the while never changes.
oh right
i dont get what u mean
"changing i"
?
unlike for loops, while doesnt increase the number
A for loop takes each item from the iterable (above, range(1,21)).
For a while loop you provide that mechanism.
i = i + 1
or
i+=1
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.
whats if you just set the loop as while i <= 20:
Good, setting i=1
so while i is less than or equal to 20
still it doesn't work
Now add code to the while loop to add 1 to i each time.
or i could just
hm?
Yay.
why do i need to set i = 1
Do you understand why it's better?
at start
the while loop version?
ye less code
more efficient?
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.
yknow you can remove the if
Essentially the same.
each even number is +2 the before one
im gonna be honest
for this while loop one
i used gpt
for help
i thought it would guide me
its okay
it just gives the answer
i use it all the time
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.
just tell us what you're trying to make with words and then translate it into python
i +=1
Right. Why is that important?
i have no clue
Ok. What does it do?
wait lemme explain
shouldnt that be a logic error
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.
Yes.
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.
oh
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.
i have a question
how did u learn this logic
not logic
chain of reasoning
to solve a problem
i am confused as hell
I already thought this way when I learnt to programme.
my thinking process is fixed
Basicly: you're telling Python exaclty what needs to be done. Leave nothing out.
uhh
this one i kinda get now
do you know random?
i've used randit
and make a little game
trying to make number guessing game
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.
alright
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
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
Oh wait until you write a complicated bit of SQL.
oh...
SQL's hard enough that we (programmers) often use ORMs (object relation managers) just to avoid writing it ๐
@silk monolith
ye?
make a number guessing game
BUT
first
tell us what your plan is
how will you make it
Describe it in english to us first.
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
hm
a long range would be better
like 1-100
also tell the player if their number is higher or lower
i've almost done it
btw
how do i use \n
new line
omg i did it
i think
@flint garnet
@ruby forge
chatgpt?
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.
like i didnt have while loop initially
wdym
To see what's going on, add:
print("randomGuess", randomGuess)
after the randomGuess = line.
Just do it once, before the loop.
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
nice
can you give me a problem where i have to use some looping
together
just tell me
another idea: print a multiplication table
hm
ah
yeah good idea
like?
(as a table with columns and rows if you get fancy)
yup
yes,but in your terminal
ah i see
btw
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"
pro tip: never compare yourself to others
academic scholar as well
mhm
i kinda do that to a lot tho
like this year i had some pretty good grades
even got an A in comp sci
that's great. But you shouldn't compare yourself to others.
you sure?
i am sure. what's the point.
well you have rows
and then columns
lets set both to 12
so we have 2 nested for loops
yes
have you used loops in loops?
why does this give me an error
oh
end is for strings
ok i know now
range(start, stop, step)
Hey @silk monolith!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
ah
for column in range(13):
for row in range(13):
print(column, end = " ")
print(row)
its correct
it's not
the formatting isnt tho
what does end="" do?
not wrong by python
with what?
test, try
test, try
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
answer this
you have "test, try"
ye
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
?
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()
Hey @silk monolith!
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!')```
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.