Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't solve this for loop

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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 + ">")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading