Can't solve this for loop

Question:

So I’m a beginner and I need to write a code that prints a x-y graph.
Here is my code:

dimx = int(input('lengte van de x-as: '))
dimy = int(input('lengte van de y-as: '))
b = int(input('b: '))
print("^")
for x in range(dimy):
    print("|")
    if x == b+1:
        for x in range(dimx):
            print("-",end="")
print("+"+"-"*dimx + ">")

The problem I have is my output prints out in the wrong order:

^
|
|
|
|
|
|
------------------------------|
|
|
|
+------------------------------>

What I need is:

^
|
|
|
|
|
|
|------------------------------
|
|
|
+------------------------------>
Asked By: FieldyScop

||

Answers:

You used print("|") which without end = "" will print a new line afterwards unconditionally
You can instead print the newline at the end using an empty print()

dimx = int(input('lengte van de x-as: '))
dimy = int(input('lengte van de y-as: '))
b = int(input('b: '))
print("^")
for x in range(dimy):
    print("|",end="") # print without newline
    if x == b+1:
        for x in range(dimx):
            print("-",end="")
    print() # print newline here
print("+"+"-"*dimx + ">")
Answered By: SollyBunny

The last | is from the next line because there’s no newline after ------------------. Add a | with no newline before the for loop, and add a newline at the end.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.