So I'm assuming here that fp(x) or f(x) are functions that don't return anything.
Take a look at this,
def fp(x):
print(x)
print(fp(10))
You might think that the output of this would be 10 but you get this,
10
None
Take a look at this function,
def fp():
pass
print(fp())
Output:
None
Functions by default return a None type. So in your case when you are doing abs(fp(x)) the problem seems to be that you are doing abs() on None that's why you get the error.
Look at this,
def fp():
pass
abs(fp())
Output:
TypeError: bad operand type for abs(): 'NoneType' python error
So add a return statement to the function and you can see the error will be gone
def fp(x):
return x
some_value = abs(fp(-10.5))
print(some_value)
Output:
10.5
Now the error is gone.
Answer from void on Stack OverflowAPI Integration - TypeError: bad operand type for abs(): 'NoneType'
python - bad operand type for abs(): 'list' - Stack Overflow
Python absolute value
[Python 3.4] Making a simple program to print the absolute value of integer but coming up with an error
I see you've already imported numpy because you're using np.linspace in the code. You are probably confusing numpy's abs, which will happily work on lists and arrays, with __builtin__.abs, which only works for scalars.
Change this:
abs(wf[0:100])
To this:
np.abs(wf[0:100])
I gather that you want abs applied to each member of the list slice along with some other computation, since you use slice notation. That's easy with a list comprehension.
plot(sf, [2.0/numSamples * abs(element) for element in wf[0:100]]);
print("Enter a integer:") var1=input() print(abs(var1))
this is the error I get when I run it
TypeError: bad operand type for abs(): 'str'
I remember that book... you're making a custom template filter right? You need to convert the value being passed in to locale.currency from a string to an int/decimal/float.
Best practice to avoid rounding errors with currency is to use the decimal package.
Import the decimal package and pass your value through the Decimal function to fix the issue.
from decimal import Decimal
value = Decimal(value)
So your code should look like this:
from django import template
import locale
from decimal import Decimal
register = template.Library()
@register.filter(name='currency')
def currency(value):
try:
locale.setlocale(locale.LC_ALL,'en_US.UTF-8')
except:
locale.setlocale(locale.LC_ALL,'')
value = Decimal(value)
loc = locale.localeconv()
return locale.currency(value, loc['currency_symbol'], grouping=True)
From the python docs.
abs()
Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned.
The error raised suggest you are passing a str argument to the abs() method where as it expects the choice of above mentioned arguments. Solution would be to explicitly pass an integer object to the abs method.
Example:
>>>number = "1"
>>>print abs(int(number))
1
>>>