Home > Software engineering >  Changing the numbers in a list to half their value
Changing the numbers in a list to half their value

Time:09-27

I'm trying to change the numbers in a list from any given list to half their values. I do not know what the list is going to be when I start, just need to assert that the function is working after.

Here is what I have so far:

def halve_values(a): for index in range(len(a)): index /= 2 return index

a = [1, 2, 3] print(halve_values(a))

The problem for me is that the list can be changed so I cannot define list a at the beginning. The program has to work if the list is given after for example: a = [1, 4, 5, 6, 8, 13]

What am I doing wrong?

Thank you! :)

CodePudding user response:

def half_values(a):
     return [x/2 for x in a]

a = [1, 4, 5, 6, 8, 13]
b=half_values(a)

print(b)

CodePudding user response:

def half_values(a): return [x/2 for x in a]

  • Related