How to declare and fill an empty array [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got an error using numpy.zeros, it seems like my value a can't be filled since i got an error:
track=2
a=np.zeros(shape=(3,2))
eps_real=a(Cp-0.5,2)/2*3.14*track
eps_imag=a(Cp-0.5,2*track)/2*3.14*track
tau=a(Cp-1,2)
print tau
My error when i ran is:
Traceback (most recent call last):
File "Main.py", line 35, in <module>
eps_real=a(Cp-0.5,2)/2*3.14*track
TypeError: 'numpy.ndarray' object is not callable

Collection members in Python use square brackets ([]), not parentheses. So your code should be:
eps_real=a[Cp-0.5,2]/2*3.14*track
eps_imag=a[Cp-0.5,2*track]/2*3.14*track
tau=a[Cp-1,2]
Parentheses are used for calling functions, hence the error message object is not callable

Related

I keep getting errors with an "if var.startswith" statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm trying to make it so that I can input someone into console and have it set a variable to it, and every time with the if statement it gives the error
File "main.py", line 58, in <module>
if meInput.startswith("%send"):
AttributeError: 'builtin_function_or_method' object has no attribute 'startswith'
Here's the code:
if input.startswith("%send"):
myinput = input.split(" ", 2)[2]
channel = client.get_channel(12324234183172)
I've tried putting it into a variable such as variable = input then changing the if statement to match the variable, but it does the same thing.
Read the error message carefully! It is telling you that input is not a string, but a function — a function that would return a string if you called it, but you didn’t. Try this instead:
if input().startswith("%send"):
Note the parentheses. That is how you call a function in Python, and in most other languages.

How to fix the error :'range' object is not callable in python3.6 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
my code looks like:
list_var = ['rh','temp','tl','Tt','DPD','PAR']
for L in range(1, len(list_var)):
for subset in itertools.combinations(list_var, L):
f = 'inf ~ {} + C(area)'.format(' * '.join(list(subset)))
error 'range' object is not callable jumped up even I changed len(list_var) into a number.
Can you identify the problem and fix it?
Thank you in advance!!!
I can reproduce the issue when assigning the range name to a range instanciated object:
>>> range = range(10)
>>> range(1)
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
TypeError: 'range' object is not callable
you probably reassigned the name earlier in your code, triggering this exact error.
A quick & dirty fix is:
del range

Creating a (2,2,3) Numpy array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm simply trying to create a (2,2,3) Numpy array, and have done the following:
a = np.array[[[1,2,3],
[4,5,6]],
[[6,7,8],
[9,10,11]]]
I however get the following error:
File "xyz.py", line 6, in <module>
[9,10,11]]]
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
How can I solve this issue?
Thanks.
You forgot parens. You want
a = numpy.array([[[1,2,3],
[4,5,6]],
[[6,7,8],
[9,10,11]]])
You got the "no attribute '__getitem__'" error because putting square brackets after a symbol means "call this object's __getitem__() method with the stuff inside the brackets", this is what's happening when you do a simple dictionary lookup:
>>> a = {1: 2}
>>> a[1]
2
>>> a.__getitem__(1)
2

AttributeError: 'str' object has no attribute '__name__' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is causing this error? Isn't it possible commenting out lines like this inside code?
for i in (Class_1, """Class_2, Class_3"""):
name = i.__name__
Class_1, Class_2 and Class_3 are classes declared before the upper code.
Error output:
> Traceback (most recent call last):
File "", line 2, in <module>
name = i.__name__
AttributeError: 'str' object has no attribute '__name__'
Process finished with exit code 1
Error message line edited to fit the example code
Remove the triple-quoted string """Class_2, Class_3""" to avoid iterating over it which is what you're doing in this case so it looks like for i in (Class_1,) (parenthesis are optional).
It seems you want to comment out those unnecessary sides, but please note that those triple-quotes strings technically aren't comments, so they can still affect the script in some areas you didn't intend.
What do you mean by
for i in (Class_1, """Class_2, Class_3"""):
When you iterate over this tuple, the second element is a string, thus causing the error.

Why will this function not open my file? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
def open_file(filename):
file_open= open(filename,"r")
return file_open
When I try and call the function I get the following results:
>>> open_file(random.txt)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
open_file(random.txt)
NameError: name 'random' is not defined
try
open_file('random.txt')
Strings in Python need to be quoted.
random is being interpreted as an object, and is undefined.
You forgot quotes:
open_file('random.txt')
python thinks random is an object, which obviously you didn't define. The quotes make it a string.
you just need to input the filename as a string; here's how it must be done:
>>> open_file('random.txt')
note that your function works just fine, all you need to do is call it properly.

Categories