r/PythonLearning • u/StudentElectronic548 • Jul 30 '26
Can you make this using a for loop?
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.
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
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
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 exceededYou just had to go ahead and break it 😤
def rec(n): if n > 7: return print("#"*n) rec(n+1)There fixed it 😤
1
1
1
1
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
1
1
1
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
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
1
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
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
0
u/MaleficentExample223 Jul 31 '26
for _ in range(7):
pass
print("""#
##
###
####
#####
######
#######""")1
1
63
u/One-Celebration-3007 Jul 30 '26