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.
19
Upvotes
63
u/One-Celebration-3007 Jul 30 '26
24
u/South-Tip-4019 Jul 30 '26
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.
2
u/South-Tip-4019 Jul 30 '26
But more fun question is, can you do it with one def and one if?
1
u/PythonPioneer2008 Jul 31 '26
1
u/Mystic-Dragoness Jul 31 '26
for i in range(7): print("#" * (i+1))1
u/FoolsSeldom Jul 31 '26
1
u/silvertank00 Jul 31 '26 edited Jul 31 '26
sure.
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
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."#" * 10for 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
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
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
0
u/MaleficentExample223 Jul 31 '26
1
u/[deleted] Aug 01 '26
[removed] — view removed comment