Home > Software engineering >  How to pass a range of numbers through a function?
How to pass a range of numbers through a function?

Time:01-03

I know that similar questions have been asked before, but I am a beginner at python and I don't know what I'm doing wrong as it keeps giving me this error: unsupported operand type(s) for: 'int' and 'range'.

I am trying to pass several values of V (from 50 to 500000) to give me back the values of E through this equation: y[i 1] = 2 * y[i] - y[i-1] (-2*(dx**2) * (E - V) * y[i]).

This is what I have done so far:

import numpy as np

dx = 0.001
xlim = 500000
y = np.arange(xlim 1)
V = range(50, 500000)

def func(E):
  for i in range(50, 500000):
    y[i 1] = 2 * y[i] - y[i-1]   (-2*(dx**2) * (E - V) * y[i])
  return y[i 1]
  
a = map(func, V)

print(list(a))

CodePudding user response:

Inside your function call, you have (E - V), which is causing your exception.

E is set to one (integer) value from V via the map() call. So you're trying to do subtraction between an integer and a range. The error is expected.

If you're trying to do a one-for-one correlation between indeces/lists, maybe you can change:

(E - V)

to:

(E - list(V)[i])

This should make the error go away, but it's poor coding and badly performing. But hopefully it can give you direction if it's along the lines of what you're trying to do. If it is, you can cast the range to a list once at the beginning, and then use that casting instead of V in the loop.

You may also need to reassess your equation to make sure the logic is correct.

CodePudding user response:

Looks like you need to debugging help. You say "it keeps giving me this error", which means you don't understand the error, and you keep trying the samething, or something similar. That's like banging your head against a wall and then complaining it keeps hurting :)

Your function - with a smaller range. No need to use big ranges before the code works:

In [525]: def func(E):
     ...:   for i in range(1,4):
     ...:     y[i 1] = 2 * y[i] - y[i-1]   (-2*(dx**2) * (E - V) * y[i])
     ...:   return y[i 1]
     ...: 
In [526]: dx=0.001
In [527]: y = np.arange(6)
In [528]: V = range(1,4)

Lets try the function with just 1 number. That has to work before passing the whole range via map.

In [529]: func(1)
Traceback (most recent call last):
  File "<ipython-input-529-9ed089a72395>", line 1, in <module>
    func(1)
  File "<ipython-input-525-9fc020925ab9>", line 3, in func
    y[i 1] = 2 * y[i] - y[i-1]   (-2*(dx**2) * (E - V) * y[i])
TypeError: unsupported operand type(s) for -: 'int' and 'range'

That's the error you keep getting. The error has nothing to do with the map(func, V).

So where's the range in your function? V is a range, right? So the function is trying to do:

In [530]: 1 - V
Traceback (most recent call last):
  File "<ipython-input-530-aa851a38238f>", line 1, in <module>
    1 - V
TypeError: unsupported operand type(s) for -: 'int' and 'range'

Same error. When you get an error in a long calculation, you need to isolate the step that's giving problem. You can do that splitting the calculation into multiple lines, or in this case, paying attention to the error message.

What if we expand the range into a full list:

In [531]: V = list(V)
In [532]: 1 - V
Traceback (most recent call last):
  File "<ipython-input-532-51e70f02e628>", line 1, in <module>
    1 - V
TypeError: unsupported operand type(s) for -: 'int' and 'list'

Addition is not defined for list either (expect that list1 list2 concatenates)

Element-wise addition is defined for a numpy array:

In [533]: V = np.array(V)
In [534]: 1 - V
Out[534]: array([ 0, -1, -2])

But when we try your function with that array V:

In [535]: func(1)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "<ipython-input-535-9ed089a72395>", line 1, in <module>
    func(1)
  File "<ipython-input-525-9fc020925ab9>", line 3, in func
    y[i 1] = 2 * y[i] - y[i-1]   (-2*(dx**2) * (E - V) * y[i])
ValueError: setting an array element with a sequence.

E-V is an array of numbers as shown in [534]. That means the full calculation also produces an array. If that isn't obvious, you haven't read enough basic numpy.

But the y[i 1] is the slot for just one number. Hence the ValueError.

I'll quit at this point because I don't know what you are trying to do, and I won't try guessing.

  • Related