Why is this code returning as invalid syntax on Phyton 3.6.0? Code is supposed t
ID: 3798731 • Letter: W
Question
Why is this code returning as invalid syntax on Phyton 3.6.0?
Code is supposed to implement a function with Python protoype (def bhatt_dist(D1, D2, n) which computes the Bhattacharyya distance between D1 and D2.
D1 = [1/2, 1/2] and D2 = [59/100, 41/100] should be used as a test case (the correct output is given above).
The math library needs to be included which is: import math in Python.
The main function that is needed from math is the natural logarithm, which is denoted as math.log .
import math
def bhatt_dist(D1, D2, n):
BCSum = 0
for i in range(n):
BCSum += math.sqrt(D1[i] * D2[i])
DBValue = - math.log(BCSum)
return DBValue
D1 = [1 , 2, 1, 2]
D2 = [ 59 ,100, 41, 100]
print bhatt_dist(D1, D2, 2)
Explanation / Answer
In python 3.x, print statement must contains parenthesis. Compiled code is given as:
import math
def bhatt_dist(D1, D2, n):
BCSum = 0
for i in range(n):
BCSum += math.sqrt(D1[i] * D2[i])
DBValue = - math.log(BCSum)
return DBValue
D1 = [1 , 2, 1, 2]
D2 = [ 59 ,100, 41, 100]
print (bhatt_dist(D1, D2, 2))
Output:
-3.0829773527647757
Hope it helps, do give your response.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.