0
How do you square python tuples?
Stuck on a python pro problem here. It wants me to square the tuple values, then add them together and finally square root the whole number. I have tried two different methods in the attached code but each time I get a value error saying it can’t do that to a tuple. How do I fix my code? This one is a challenge. https://code.sololearn.com/c995D0o8qiWJ/?ref=app
3 Antworten
+ 4
This is how tuples are squared in python.
import math
p1 = (25, 64)
print(math.sqrt(p1[0]))
print(math.sqrt(p1[1]))
So your code needs to be,
import math
p1 = (23, -88)
p2 = (6, 42)
print(math.sqrt((p1[0]**2+p1[1]**2)+(p2[0]**2+p2[1]**2)))
+ 1
you could also do:
print(math.sqrt(sum(v*v for v in p1+p2)))
0
correct format:
print(math.sqrt(((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2)))