Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 205149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:31:51+00:00 2026-05-11T17:31:51+00:00

In my previous question , Andrew Jaffe writes : In addition to all of

  • 0

In my previous question, Andrew Jaffe writes:

In addition to all of the other hints and tips, I think you’re missing something crucial: your functions actually need to return something.
When you create autoparts() or splittext(), the idea is that this will be a function that you can call, and it can (and should) give something back.
Once you figure out the output that you want your function to have, you need to put it in a return statement.

def autoparts():
    parts_dict = {}
    list_of_parts = open('list_of_parts.txt', 'r')
    
    for line in list_of_parts:
        k, v = line.split()
        parts_dict[k] = v

    print(parts_dict)

>>> autoparts()
{'part A': 1, 'part B': 2, ...}

This function creates a dictionary, but it does not return something. However, since I added the print, the output of the function is shown when I run the function. What is the difference between returning something and printing it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:31:51+00:00Added an answer on May 11, 2026 at 5:31 pm

    print simply prints out the structure to your output device (normally the console). Nothing more. To return it from your function, you would do:

    def autoparts():
        parts_dict = {}
        list_of_parts = open('list_of_parts.txt', 'r')
        for line in list_of_parts:
            k, v = line.split()
            parts_dict[k] = v
        return parts_dict
    

    Why return? Well if you don’t, that dictionary dies (gets garbage collected) and is no longer accessible as soon as this function call ends. If you return the value, you can do other stuff with it. Such as:

    my_auto_parts = autoparts() 
    print(my_auto_parts['engine']) 
    

    See what happened? autoparts() was called and it returned the parts_dict and we stored it into the my_auto_parts variable. Now we can use this variable to access the dictionary object and it continues to live even though the function call is over. We then printed out the object in the dictionary with the key 'engine'.

    For a good tutorial, check out dive into python. It’s free and very easy to follow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Previous question which links onto this and has any addition code ref should I
In previous question of mine, someone had meantioned that using Semaphores were expensive in
This question is related to a previous question of mine That's my current code
This question follows on from a previous question, that has raised a further issue.
This question is related to my previous question How to generate Cartesian Coordinate (x,y)
This question is related to my previous question . The storyline: I have an
This question is a continuation of my previous question here zend models architecture (big
This question is in continuation to my previous question . I am trying to
This question is related to my previous question - MySQL query to show records
This question follows on from a previous question. However stackoverflow presents me from commenting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.