r/PythonLearning • u/Chicken_Nuggies123 • Sep 05 '25
Help Request Could anyone try to help me figure out how I'm supposed to solve this?
I assume it wants me to use for loops because that's what this unit was about. I have quite a bit of experience with python and programming in general but this just seems insane to me being at the almost very start of an introductory python course. I have genuinely no idea how I would do this so any help is greatly appreciated.
2
u/_ZeroHat_ Sep 07 '25 edited Sep 08 '25
I solved it like this:
def draw_hex(
size
):
stars =
size
spaces =
size
- 1
buff = [i for i in range(0, spaces)] + [i for i in range(spaces, -1, -1)]
for i in buff:
print(" " * (spaces - i),
end
="")
if i % 2 == 0:
line = ["* " for i in range(stars)]
for j in range(i//2):
line.insert(j*2+1, " ")
line.insert(j*-2-1, " ")
else:
line = []
for j in range(i//2+1):
line.append("*")
line.append(" ")
line.append(" " * ((
size
- i) + (max(buff) - 1 - i)))
line.append("*")
for j in range(i//2):
line.append(" ")
line.append("*")
print("".join(line))
draw_hex(5)
draw_hex(7)
draw_hex(8) # also works wich odd numbers1
u/Chicken_Nuggies123 Sep 08 '25
What's going on with the
line.append(" " * ((size - i) + (max(buff) - 1 - i)))part? I'm not really understanding how you got that math lmao
2
u/Simiouch Sep 06 '25 edited Sep 06 '25
I took this challenge and implemented it in half an hour :)
My key insight was that you can start on the left dot of a hexagon, and walk n times in one of 6 directions! I added all these location to a set, for each hexagon's starting point. And then just printed "*" when it's in the set!
FYI, this works for all uneven n and I did not use any GPT/vibecoding

n = 5
# Six directions you can "walk" in clock-wise order (x,y)
dir = [(1, -1), (2, 0), (1, 1), (-1, 1), (-2, 0), (-1,-1)]
def generate_points(n):
points = set()
# e.g. n = 5 -> x = 0,2,4
for x in [i for i in range(n) if i % 2 == 0]:
sp = (x*2, n - 1) # startpoint in middle line
points.add(sp)
for d in dir:
for w in range(n - x - 1):
# walk w steps in direction d
sp = (sp[0] + d[0], sp[1] + d[1])
points.add(sp)
return points
points = generate_points(n)
width = 4*n - 3 # deduced from image xd
height = 2*n - 1
for j in range(height):
for i in range(width):
character = "*" if (i, j) in points else " "
print(character, end="")
print()1
u/Simiouch Sep 06 '25
u/Chicken_Nuggies123 There you go homie :)
1
u/null_false Sep 07 '25
Can you explain the direction steps a bit more? Not sure I fully understand the logic behind the set
2
1
u/Fit_Mushroom_4087 Sep 05 '25
Think of it as a grid, you know coordinates? You just need to output a list of strings with spaces or “*” in them. What are the dimensions of this grid, i.e. how many strings and what length. If you generate strings one by one, what changes between iterations ? That’s what the “for loop” is for
1
u/Defiant-Letterhead47 Sep 06 '25
Have you been taught the turtle library. Cos you can actually do that with the library
1
u/Algoartist Sep 06 '25
Try to figure out the sequences of spaces and stars.
ChatGPT has serious problems to solve it btw
1
1
u/iamemhn Sep 06 '25
I don't program in Python.
You have to print left to right, top to bottom. Now observe and think.
The example implicitly shows what to do for sizes 1, 3, and 5. So it should work for 7, 9, and so on... Would it work for even sizes? I don't think so, so I'd focus on making it work for odd sizes first.
So, think top down and odd sizes only, great...
Regardless of the size, the middle line half way down is unique and it will always have as many stars as the size. The spacing is also proportional to the size.
I think I can code how to print the middle line. Easy win.
Now, the upper and lower halves are symmetrical respective to the middle line. Whatever solution I come up with to print the upper half, I can use «in reverse» to get the lower half. Neat!
Howany lines are on the upper half real I've to the size?
Now, let's look at the spacing from the left column to the first star, relative to the size, and which line I'm trying to print...
Programming is thinking. Anyone saying otherwise is an idiot or is trying to sell you AI.
-1
0
Sep 05 '25
[deleted]
4
u/Chicken_Nuggies123 Sep 05 '25
If I were to do it by hand I would draw the inner most hexagon and move outwards until I reach the desired size. But how would I do that in code? Since it's being printed in the terminal shouldn't it work from the top to the bottom?
2
u/fynn34 Sep 05 '25
You have a 2 dimensional display here, are there any data types that could represent that? If so, you could start from the middle and work out and still have it work
1
u/Chicken_Nuggies123 Sep 05 '25
There are multiline strings and multidimensional lists, but I'm not really sure how I would use those. And we also haven't reached the units with those things in them so I assume we aren't expected to need them
1
u/BranchLatter4294 Sep 05 '25
Think in terms of printing just one character at a time. The character can either be a * or a space. Just think in terms of how many * and how many spaces you need to print each time through the loop.
1
u/Kqyxzoj Sep 05 '25
Yup, this.
I just built some quick and dirty code, and you can get it done with simple building blocks.
One way to build each line is just from three building blocks: " ", " " and "* ". So one space, two spaces and *+space.
Each line is an alternating sequence of a number of spaces, then a number of *, then more spaces, more stars, etc.
Each line starts with a certain amount of spaces. Try to work this out for each line.
Just write down those numbers for your example cases. You will probably see a pattern emerging. Then you recreate that pattern yourself with for example a for-loop.
1
Sep 05 '25
Of course you have to think like a program when doing this. So even if you start from the center, you have to eventually write if from top to bottom. You may use a trick, like first write in a matrix in any order you wish, then print the matrix row by row, but I believe you are supposed to write it directly. Not as hard as it seems though. Draw it first, then find patterns. Count blanks carefully.
1
u/BranchLatter4294 Sep 05 '25
Since you know that print output appears top down, then you have to think in those terms. Not inside out.
1
u/Kqyxzoj Sep 05 '25
In this case top to bottom or bottom to top doesn't matter since it is symmetrical.
3
u/BranchLatter4294 Sep 05 '25
I would start by practicing simpler shapes with for loops, printing just one character at a time. Try
If you can do these basic shapes, you should be able to figure out the hexagon. If you can't figure out a problem, simplify it.