I'm new to python and trying to do a HW assignment and it keeps throwing this error back at me (in iPython Notebook):
"TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'float'"
Here's the formula i'm trying to imitate: h(x)=(1/√2π)*e^−(1/2)^x^2
import numpy as np
import math
from math import *
def h_scalar(x):
return (1 / sqrt(2 * pi)) * exp(-.50** x ** 2.00)
I'm trying to get the same out put as my teacher.
l = [-10.0, -1.0, 0.0, 1.0, 10.0] #
print h_scalar(l)
>>> [1.4867195147342977e-06, 0.24197072451914337, 0.3989422804014327,
0.24197072451914337, 1.4867195147342977e-06]
Here's the teachers question btw:
First of all, write a function called h_scalar. The function should expect a list (or in general an item that is iterable). It will expect that the list contains floating point values. It should return a new list of floats that are the h(x) for each x value in the input list. You should name your function h_scalar()
-.50 * x ** 2.00. 2.) You cannot broadcast an operation like**2.00across a standard python array. To do this, convertxto anp.array. 3.) If you don't want to use numpy, consider using aforloop to compute the formula for each element inx. Store these values in another array, which you then return. Alternatively, use a list comprehension.