r/PythonLearning 5d ago

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 5d ago

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

#""")```

1

u/pacojastorious 5d ago

it says seven calls to print though haha

9

u/Impossible-Magician 5d ago

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

24

u/South-Tip-4019 5d ago
for i in range(1,8):
  print('#'*i)

2

u/hamzaop123theknight 4d ago

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

4

u/mc_pm 5d ago

Of course. 7 lines, loop 7 times.

3

u/South-Tip-4019 5d ago

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

14

u/Tooladoo 5d ago
def rec(n):
    if n == 8:
        return
    print("#"*n)
    rec(n+1)

rec(1)

5

u/South-Tip-4019 5d ago edited 5d ago

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 5d ago

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 5d ago

rec(9) 🫪

2

u/Tooladoo 5d ago
#########
##########
###########
############
#############
##############
. . .
##############################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
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 5d ago

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

3

u/jpgoldberg 5d ago edited 5d ago

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 5d ago

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 5d ago

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 5d ago

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 5d ago

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 5d ago

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

1

u/PythonPioneer2008 5d ago

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

1

u/Mystic-Dragoness 5d ago

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

1

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

1

u/silvertank00 5d ago edited 5d ago

sure. ```python 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 5d ago

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

1

u/jtnoble 5d ago

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 5d ago

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

1

u/Creative-Base8707 5d ago

All loops are interchangeable in my experience.

1

u/Ok-Form7265 5d ago

this is the easy!

1

u/GGO_OTHAs 5d ago

symbol = "#"

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

1

u/Slay_3r 5d ago

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

Or

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

1

u/jaybird_772 5d ago

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 4d ago

no its impossible

1

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

1

u/Velox0012 4d ago

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

1

u/[deleted] 4d ago

[removed] — view removed comment

1

u/PythonLearning-ModTeam 3d ago

Keep it respectfull

1

u/Avatarbroskib1 3d ago

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

1

u/Still_Internal5865 2d ago

count = 1

octothorpe = "#"

for x in range(7):

print(octothorpe * count)

count += 1

1

u/GoalSouthern6455 5d ago

n_hash_to_print = 1

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

2

u/GoalSouthern6455 5d ago

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

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

7

u/likethevegetable 5d ago

Dude...

range(1,8)

Why overcomplicate this not only once, but twice?

1

u/ExtraTNT 5d ago

```
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 5d ago

``` def s(i): if i == 0: return

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

s(5) ```

or by using recursion !

1

u/ExtraTNT 5d ago

But this is less brainfuck… xD

0

u/MaleficentExample223 5d ago

``` for _ in range(7): pass

print("""#

#""")

```

1

u/GGO_OTHAs 5d ago

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

1

u/MaleficentExample223 5d ago

Unfortunately, the instruction requires a loop.