Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions
#1
Hello everyone
The only problem I have here is printing the distance between two points. The program asks the user to input 2 values and then appends those values together as a tuple to a list. Then when I need the distance, it is supposed to take the first value and the second and compute the distance. Then the second and the third. Then the third and the fourth and so on for as many tuples are in the list. However it only prints the distance of the first and second points. I know what I have right now doesn't work. Any ideas of how to do it better are greatly appreciated.
my program:
import math
from location import where_is_xy
#Below is the "main" function which acts as the entry point
l =[]
b = []
list4 = []
list5 = []
list6 = []
if __name__ == '__main__':
    flag = False
    try:
        while flag == False:
            x, y = [int(x) for x in input("Enter two numbers here seperated by space: ").split()]
            l.append((x,y))
            result = where_is_xy(x, y)
            list4.append((x, y))
                      
    except Exception:
        flag = True
        print("Points:", l)
        for i in range(0, len(l)):
            print(l[i], "%5s" % result[i])

            
    print("Distance:")
    for i in range(0, len(l) - 1):
        print(l[i], l[i + 1], "=", "{:,.2f}".format(math.sqrt(((l[0][0] - l[1][0]) ** 2) + ((l[0][1] - l[1][1]) ** 2))))
location is a function that has no impact on the distance so I'm not posting it here.
Reply


Messages In This Thread
Functions - by Leo - Mar-11-2022, 01:51 AM
RE: Functions - by Leo - Mar-11-2022, 03:56 AM
RE: Functions - by ndc85430 - Mar-11-2022, 06:55 AM
RE: Functions - by deanhystad - Mar-11-2022, 07:20 AM
RE: Functions - by DeaD_EyE - Mar-11-2022, 10:30 AM
RE: Functions - by deanhystad - Mar-11-2022, 06:54 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020