How do you square python tuples? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

12th Feb 2021, 2:58 AM
Steve Barone
Steve Barone - avatar
3 Answers
+ 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)))
12th Feb 2021, 4:07 AM
Simba
Simba - avatar
+ 1
you could also do: print(math.sqrt(sum(v*v for v in p1+p2)))
12th Feb 2021, 5:15 AM
visph
visph - avatar
0
correct format: print(math.sqrt(((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2)))
19th Jul 2022, 2:21 AM
Md Jahid Hasan
Md Jahid Hasan - avatar