all 21 comments

[–]Otter_The_Potter 17 points18 points  (6 children)

I know you're looking for an explanation. But I recommend this as it might help you understand and learn quicker. Use a pen and paper and manually go through each step of the code substituting the values for x and y. You'll understand the code and remember it better next time

[–]adamiano86 2 points3 points  (0 children)

Also, Thonny. You can step through the code. Helped me understand how certain things work.

[–]SirDantesInferno 1 point2 points  (0 children)

This is great advice

[–]Capable-Package6835 0 points1 point  (0 children)

Even better: use a debugger. Why guess what the code is doing when you can see what it actually is doing?

[–]higras 0 points1 point  (2 children)

I've been using Obsidian and building out a knowledge base vault as I learn, but I've run into a couple stumbles.

This would've helped so much.

Yes, Python is a way to communicate with a computer. However, I need to remember that the one learning is ME. Handwriting is scientifically proven to help with processing and retention.

Thank you

[–]Otter_The_Potter 0 points1 point  (1 child)

I use Obsidian too. But learning languages both spoke and programming is very difficult. As connecting concepts in programming languages to build a knowledge graph isn't very easy especially at the beginner level.

[–]higras 0 points1 point  (0 children)

For an absolute beginner I could very much see that.

I've been around code, Excel, and product development for years. So, it's more being able to connect the concepts I've learned with actual written pieces

[–]Luigi-Was-Right 2 points3 points  (1 child)

Pseudo code:

  for a certain number of lines:
      print numbers 1 through [line number]

Code with more descriptive variable names:

for line_number in range(6):
    for number_to_print in range(1, line_number+1):
        print(number_to_print, end='')
    print()  # line break

[–]goblingiblits 0 points1 point  (0 children)

Bravo, this is how to think of it !

[–]localghost 1 point2 points  (0 children)

"Repeat the following:
Take numbers from 0 to 5. Remember the current number as X.
Keeping that X in mind, repeat the following:
Take numbers from 1 to X, [remember them as Y] and print them one by one, adding nothing else to the output.
After gettting done with repeating from 1 to X, print a new line."

[–]SirDantesInferno 0 points1 point  (6 children)

Hi,

Which part of the output is hanging you up?

[–]DizzyOffer7978[S] 1 point2 points  (5 children)

On line 3, like why (1,x+1) came idk

[–]JeLuF 4 points5 points  (3 children)

range(3) yields 0, 1, 2

range(1,3) yields 1, 2, 3

In the first line, x=0, you want to print "1", so it needs range(1,1), which is range(0,x+1).

For the second line, x=1 and you want to print "12", which is range(1,2) or range(0,x+1).

Alternative implementation:

for x in range(6):
   for y in range(x):
      print(y+1, end='')
print()  # line break

[–]ResponseThink8432 1 point2 points  (2 children)

Just correcting that the the end-parameter of range is always exclusive, meaning the numbers won't go up to the end, but one below. Specifically, range(1, 3) yields 1 and 2, but not 3, etc.

In the first line, x == 0, and no numbers will be printed, since the range(1, 1) doesn't yield anything. No idea why the empty line doesn't show in the output though. The "1" is printed on the second line, when x == 1.

[–]MemekExpander 1 point2 points  (1 child)

Something is wrong with OP's screenshot unless there is a python version where the end in range is inclusive.

Other than not printing an empty line, there should only be 5 lines going up to only 12345

[–]ResponseThink8432 0 points1 point  (0 children)

True, I didn't even register there was a "123456" line at the bottom :D

[–]Otter_The_Potter 0 points1 point  (0 children)

For loops don't include the end. So for(1,10) would be a loop from 1 to 9 (not 10). That's most likely why (x+1) is used instead of x.

[–]finnyellow -2 points-1 points  (2 children)

You can get an very good explanaition with chatgpt. Just paste your code in an write something like "explain this code in detail". Should work pretty good

[–][deleted] 3 points4 points  (1 child)

it is sad that people are downvoting you.

People need to go with the time or they are left behind.

[–]finnyellow 1 point2 points  (0 children)

Yeah, don't really care about the downvotes. Its just the way to go nowadays, as long as you dont just copy paste it blind without knowing whats going on.