Im trying to add 5 to this vector if a number in the vector is less then 4. I feel like im pretty close but it isnt running correctly. Can anyone give me some help adding 5 to any value less then 4 in the vector?
from pylab import *
numberlist = [3.2,6,7.8,1,3,2.5]
def x(numberlist):
for num in numberlist:
if num > 4:
print(num)
else :
print(num + 5)
print x([3.2,6,7.8,1,3,2.5])
What you have does exactly what you’re describing. Looks like you’re new to python, possibly new to programming in general.
The code is a bit confusing for the following reasons.
numberlistand assign it an array of numbers.xis namednumberlistagain.x, you pass an array of the same values you assigned to the global variablenumberlistin the beginning.Important points to note:
numberlistandx‘s parameter namednumberlistare two TOTALLY DIFFERENT VARIABLES. The effect you have is that the global variablenumberlistisn’t accessible inside of your functionx. You’re not actually using the global variablenumberlistanywhere.numberlistand pass it tox, instead ofx([3.2,6,7.8,1,3,2.5]), you would just dox(numberlist).x‘snumberlistparameter to something else.mylistfor example.Taking those into account, your code is more readable as follows: