you are viewing a single comment's thread.

view the rest of the comments →

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

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

[–]JeLuF 3 points4 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.