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

Indexing does not work in my ptyhon program. Can you help me?

I don’t understand where the "none" comes from in the output of my Python program.
Can anyone help me?

Program

def printColumn(x):
    if type(x) is not list:
        x = list(range(0,5))
    for i in range(len(x)):
        print(x[i])

x = [8,24,48,2,16]  
print(printColumn(x))

Output

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

8
24
48
2
16
None

Thank you in advance

I just don’t understand why it doesn’t work

>Solution :

The function prints the values 8 thru 16.

But your main-level code has print(printColumn(x)), which means that the return value of the function is also printed. And since the function has no return statement, it returns None by default.

If you want to just call the function, without printing its result, just use printColumn(x) without the outer print() call.

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