In the following custom function:
def get_a_random_memory(length, lower_sum_range, upper_sum_range):
memory = list()
for i in range(0, length):
memory.append((2 * random.randint(0, 1) - 1))
sum = 0
for i in range(0, length):
if len(memory) == 0:
sum = memory[i]
else:
sum = sum + memory[i]
I am getting the following error.
>>> print memories.get_a_random_memory(10, 1, 10)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\omarshehab\PycharmProjects\practice\memories.py", line 28, in get_a_random_memory
if len(memory) == 0:
TypeError: 'list' object is not callable
I assume I am accessing the list variable memory correctly.
Any help please?
You could consider doing it as follows:
def get_a_random_memory(length, lower_sum_range, upper_sum_range):
memory = [(2 * random.randint(0, 1) - 1) for i in xrange(length)]
total = sum(memory)
print memory
print total
get_a_random_memory(10, 0, 0)
You should avoid using sum
as a variable as there is already a Python function with that name that performs the calculation automatically on a list.
This script would for example display:
[1, -1, -1, -1, -1, 1, 1, 1, -1, -1]
-2
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments