Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List not being returned from function

When I create a new list in my function at the end I want to return that new list to replace the values that were initially called however when the return statement runs nothing actually happens.

def fixNumbers(list):
    newList = []
    for i in list:
        if i > 255:
            newList.append(225)       
        elif i < 0:
            newList.append(0)
        else:
            newList.append(i)
    return newList
values1 = [-10, 100, 300]
fixNumbers(values1)
print(values1)
values2 = [127, 216, 280, 250, 50, 0, -3, 20]
fixNumbers(values2)
print(values2)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In last lines you can do like:

values1 = [-10, 100, 300] 
fix_numbers_1 = fixNumbers(values1)
print(fix_numbers_1)

Full code below:

def fixNumbers(list):
    newList = []
    for i in list:
        if i > 255:
            newList.append(225)       
        elif i < 0:
            newList.append(0)
        else:
            newList.append(i)
    return newList
values1 = [-10, 100, 300]
fix_numbers_1 = fixNumbers(values1)
print(fix_numbers_1)

values2 = [127, 216, 280, 250, 50, 0, -3, 20]
fix_numbers_2 = fixNumbers(values2)
print(fix_numbers_2)

Info:

  • When you call a function and it returns something we have to store that returned value in order to use it
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading