Home > database >  Can't solve this for loop
Can't solve this for loop

Time:10-09

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:

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

CodePudding user response:

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   ">")

CodePudding user response:

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.

  • Related