PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 15, 2022

[FIXED] Why does using this user defined function within print output none?

 September 15, 2022     object, printing, python     No comments   

Issue

def rev_sort(thelist):
    '''
    Reverses and then sorts the list entered
    '''
    thelist.sort()
    thelist.reverse()

def main():
    thelist = ["fun", "is", "programming"]
    # rev_sort(thelist)
    print(rev_sort(thelist))
    
main()

Calling main here outputs None, however, if I instead first use the function rev_sort and print the resulting list I get the decided output. However, I would expect it to be fine to do it like this, what's going on?


Solution

def rev_sort(thelist):
    '''
    Reverses and then sorts the list entered
    '''
    thelist.sort()  # sort function return None
    thelist.reverse()  # reverse function returns None.
    return thelist  # you need to return the list back to main. 

def main():
    thelist = ["fun", "is", "programming"]
    print(rev_sort(thelist))
    
main()


Answered By - Nesi
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing