solid
solid

Reputation: 1

how would i improve this so it would add distance to list and then add the sum

x1 = 0.00
y1 = 0.00

distList = [] 

def coord(x1, y1):
    while True:
        x2 = int(input("Enter x: ")) 
        y2 = int(input("Enter y: ")) 
        x1 = x2
        y1 = y2
        distance = ((x2 - x1)**2 + (y2 - y1)**2)**.5
        distList.append(distance)

def step():
    while True:

        if(x2 == 999 or y2==999):
            break
            print(sum(distList))
        else:
            coord(x1, y1)


coord(x1, y1)

I've been struggling to figure out how to break the loop once the user enters 999, this doesn't stop the loop for some reason and another issue is that the list distList is meant to store the distance traveled when the user enters the x and y value but it overwrites the first inputs.

Upvotes: 0

Views: 56

Answers (1)

j6m8
j6m8

Reputation: 2399

You are getting "stuck" in your coord() function's while-loop: You never call step(), so you never encounter the if check in that while-loop.

You also don't have any conditionals (e.g. if) in your coord() while loop, which means there's no way to "break out" of that loop!

Check out this slightly updated code:

x1 = 0.00
y1 = 0.00

distList = [] 

def coord(x1, y1):
    while True:
        x2 = int(input("Enter x: ")) 
        y2 = int(input("Enter y: ")) 

        # Let the user exit out here by submitting "999"
        if x2 == 999 or y2 == 999:
            break
        x1 = x2
        y1 = y2
        distance = ((x2 - x1)**2 + (y2 - y1)**2)**.5
        distList.append(distance)



coord(x1, y1)

There are a few other issues that I can see in this code; for example, if you set x1 = x2, then x2 - x1 will always be 0. See if you can figure out what to do with the x1 = x2 line to fix this problem!

Upvotes: 1

Related Questions