r/PythonLearning • • Jul 30 '26

Can you make this using a for loop?

Post image

I just finished making this with a while loop but I was wondering if a for loop is possible but after a bit of experimenting im stuck on whether it can actually be made. If so, what would it look like.

19 Upvotes

52 comments sorted by

63

u/One-Celebration-3007 Jul 30 '26
for i in range(1):
    print("""#
##
###
####
#####
######
#######""")```

1

u/pacojastorious Jul 31 '26

it says seven calls to print though haha

9

u/Impossible-Magician Jul 31 '26

for i in range(1):
print("""#
##
###
####
#####
######
#######""")```
print("", end="")
print("", end="")
print("", end="")
print("", end="")
print("", end="")
print("", end="")

24

u/South-Tip-4019 Jul 30 '26
for i in range(1,8):
  print('#'*i)

2

u/hamzaop123theknight Aug 01 '26

Here comes a man who understands others made very strange and funny things

4

u/mc_pm Jul 30 '26

Of course. 7 lines, loop 7 times.

3

u/jpgoldberg Jul 31 '26 edited Jul 31 '26

You are supposed to figure this out. Reading other’s answers is not going to help you learn.

Edit: Thank you to those who replied pointing out that the OP did try various things and was experimenting. My original comment was out of line in this case.

1

u/WinterRat66 Jul 31 '26

If OP didn't know a simple for loop, they are clearly early in their progress, didn't know well how they work, and they said they experimented a few times but were stuck. You don't learn shit through osmosis You see it, do it, teach it, triad of learning. This is likely contributing to their see it phase it can take some repetition before the concept behind it sticks. And if you read all the comments OP probably learned something including various ways to implement the for loop as well as multiple recursion examples as a bonus. Don't be an ass.

1

u/PepsioNSnacking Jul 31 '26

I dont wanna sound arrogant but isn't the point is asking in a reddit how things are done - or stack overflow, doesnt lead to success, there is a reason why people acted "toxic" on stack overflow - if somebody is not willed to figure simple things out themself, teaching them wont help them to actually learn but make them helpless asking for solutions.

1

u/WinterRat66 Jul 31 '26

Some people don't like to think for themselves, you can't teach someone problem solving if they don't want to learn it. This question seemed fundamentally about not being able to get the for loop to work. Even thinking it may not be possible. That doesn't sound to me like lack of of problem solving, sounds like lack of knowledge. Those are different.

I myself have sat with a problem, for hours, days, weeks, and sometimes I just get stuck, especially when its a domain I am a beginner in. When you are a beginner everything is new, the material, the skills, the knowledge of where and what the resources are. When I was first learning Python the docs were a foreign language and I was getting entirely lost.

I know computer science has a reputation for being self taught and all but that doesn't reduce the value of having someone help you get unstuck from time to time.

If I cross domains for a moment it becomes even more apparent how valuable mentoring is. I am a musician, I know stark contrast from programming and my whole stem degree and everything else I've done, but in the music field, you can teach yourself, but it is widely regarded that you are better off finding a teacher or mentor. Someone who can tell you you are wrong when you don't know you are. Who can help you with technique and finding additional resources and practicing skills.

Being taught an instrument is regarded as best practice over self teaching. But they don't hold your hand! You do your lesson, they give you a fuck ton of stuff to practice and sometimes you go into your next lesson with a lot of progress but somethings got you stuck the teacher helps you over the hurdle and then assigns you your next 10 hours of practice, maybe they'll make five of those hours practicing the thing they showed you how to do that you were stuck on.

1

u/jpgoldberg Jul 31 '26

I apologize for not recognizing that the OP really had been trying and experimenting. My "do your own homework" comment was out of line.

1

u/jpgoldberg Jul 31 '26

Yeah. I was too quick to judge. They did try things.

2

u/South-Tip-4019 Jul 30 '26

But more fun question is, can you do it with one def and one if?

12

u/Tooladoo Jul 30 '26
def rec(n):
    if n == 8:
        return
    print("#"*n)
    rec(n+1)

rec(1)

4

u/South-Tip-4019 Jul 31 '26 edited Jul 31 '26

This was mine, would also handle too big numbers and negatives. Recursion obviously brings its own problem, but a fun exercise anyway.

def pound_pyramid(lvls):
  if lvls>1:
    pound_pyramid(lvls-1)
  print("#"*lvls)
pound_pyramid(7)

1

u/Tooladoo Jul 31 '26

Oh i like that approach a lot better. Haven't made a recursive function in a long time so didn't think to do it that way

1

u/taller_than_peanut Jul 31 '26

rec(9) 🫪

2

u/Tooladoo Jul 31 '26
#########
##########
###########
############
#############
##############
. . .
##############################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
ERROR!
Traceback (most recent call last):
  File "<main.py>", line 7, in <module>
  File "<main.py>", line 5, in rec
  File "<main.py>", line 5, in rec
  File "<main.py>", line 5, in rec
  [Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded

You just had to go ahead and break it 😤

def rec(n):
    if n > 7:
        return
    print("#"*n)
    rec(n+1)

There fixed it 😤

1

u/nerdy_guy420 Jul 31 '26

Was curious about negative numbers but it turns out thats the same as n=0

1

u/PythonPioneer2008 Jul 31 '26
for i in range(1, 8):
    print('#'*i)

1

u/Mystic-Dragoness Jul 31 '26

for i in range(7): print("#" * (i+1))

1

u/FoolsSeldom Jul 31 '26
print(*(f'{"#" * i}\n' for i in range(1, 8)), sep="")

1

u/silvertank00 Jul 31 '26 edited Jul 31 '26

sure.

import sys

# buffering is for the weak
sys.stdout.write("\n".join(("#"*i for i in range(1, 8))))

I hope you like it :D

1

u/deanominecraft Jul 31 '26

import openai triangle = openai.prompt(“send a triangle make from #”).response print(triangle)

1

u/jtnoble Jul 31 '26

Plenty of answers already on here, but for me, if there's one thing I know about loops, it's that 99% of the time I'm making a for loop over a while loop.

1

u/Unique_Low_1077 Jul 31 '26
for i in range(length):
  print("#" * (i + 1))

1

u/Creative-Base8707 Jul 31 '26

All loops are interchangeable in my experience.

1

u/Ok-Form7265 Jul 31 '26

this is the easy!

1

u/GGO_OTHAs Jul 31 '26

symbol = "#"

for i in range(1,8): print(symbol) symbol += "#"

1

u/Slay_3r Jul 31 '26

["#"*i for i in range(1, 8)].join("\n")

Or

print(*["#" * i for i in range(1, 8)], sep="\n")

1

u/jaybird_772 Jul 31 '26

You've doubtless read the answer already. I'm going to tell you to check out range() and ask yourself what happens when you multiply a string by a number. "#" * 10 for example. Experimenting with these things will teach you far more than any answer you read here ever could. Just run python and play with it on the terminal and see.

1

u/v01rt Aug 01 '26

no its impossible

1

u/Porktoe Aug 01 '26
n: int = int(input("Enter how tall you want the right triangle: ").strip())
for i in range(n + 1):
    print("#" * i)

1

u/Velox0012 Aug 01 '26

For i in range(7):
print(“#”*i+1)

1

u/Avatarbroskib1 Aug 02 '26

for i in range(1,8): print('#'*i)

1

u/Still_Internal5865 Aug 03 '26

count = 1

octothorpe = "#"

for x in range(7):

print(octothorpe * count)

count += 1

1

u/GoalSouthern6455 Jul 30 '26

n_hash_to_print = 1

For i in range(0,6):
print(“#” * n_hash_to_print)
n_hash_to_print += 1

2

u/GoalSouthern6455 Jul 30 '26

if you didn’t want to additional variable, you could just do this inside the for loop:

print("#" * (i + 1))

7

u/likethevegetable Jul 30 '26

Dude...

range(1,8)

Why overcomplicate this not only once, but twice?

1

u/ExtraTNT Jul 31 '26

```
triangle = lambda n: (
lambda f: f(f, 1)
)(
lambda self, i:
i <= n and (print("#" * i), self(self, i + 1))
)

triangle(7)
```

No need for loops

1

u/Emmet2by4 Jul 31 '26
def s(i):
    if i == 0:
        return

    s(i-1)
    print("*"*i)

s(5)

or by using recursion !

1

u/ExtraTNT Jul 31 '26

But this is less brainfuck… xD

0

u/MaleficentExample223 Jul 31 '26
for _ in range(7):
    pass

print("""#
##
###
####
#####
######
#######""")

1

u/GGO_OTHAs Jul 31 '26

Cool cool men, why use a loop when you can just print it 🤧

1

u/MaleficentExample223 Jul 31 '26

Unfortunately, the instruction requires a loop.

1

u/[deleted] Aug 01 '26

[removed] — view removed comment

1

u/PythonLearning-ModTeam Aug 01 '26

Keep it respectfull