Reputation: 33
def print_distance(p1, p2):
dist = (((p2[0]-p1[0])**2)+((p2[1]-p1[1])**2))**0.5
d = print("{0:.2f}".format(dist))
result = ("Distance between ", ','.join(p1) ," and ", ','.join(p2) ," is "+ d)
return result
print_distance((0,1),(-1,-2))
TypeError: sequence item 0: expected str instance, int found I couldn't figure it out the output "Distance between (0, 1) and (-1, -2) is 3.16" I am beginner to this thing. Help me.
Upvotes: 2
Views: 83
Reputation: 421
result = ("Distance between ", ','.join(p1) ," and ", ','.join(p2) ," is "+ d)
this is line is just a bit of a mess maybe try using format the way you did elsewhere
result = "Distance between {p1} and {p2} is {d}".format(p1, p2, d)
I'd like to give you more context but the gist of it is you are using join incorrectly.
Upvotes: 2
Reputation: 11992
your tuple contains ints so you cant join them with str.join. if you wanted to do that you would need to convert them first to str's. however to get the output you mentioned you can just use an fstring
def print_distance(p1, p2):
dist = (((p2[0]-p1[0])**2)+((p2[1]-p1[1])**2))**0.5
print(f"Distance between {p1} and {p2} is {dist:.2f}")
print_distance((0,1),(-1,-2))
OUTPUT
Distance between (0, 1) and (-1, -2) is 3.16
Upvotes: 5