1

I have some practice homework with creating a function in python. One of them outputs a list of months. This is my code:

def outputlist(list):
    for list_output in list:
       print(list_output,end=" ")
    return list_output

def main():
    month = ["January", "February", "March", "April", "May", "June", "July", "August", "Setemper", "October", "November", "December"]
    print("The list of the months of the year are:", outputlist(month))

main()

I don't know why the output sentence does not follow in order, It should be:

"The list of the months of the year are: January February March April May June July August September October November December"

However, the output that I get is:

January February March April May June July August September October November December 1. The list of the months of the year are: December
8
  • 1
    Do you understand the difference between print() and return? Commented Feb 13, 2018 at 4:47
  • 1
    You could just use print("The list of the months of the year are:", *month) Commented Feb 13, 2018 at 5:07
  • @IgnacioVazquez-Abrams I'm new to python, so I only know that "return" will only return the value in the function Commented Feb 13, 2018 at 5:16
  • @JohnLaRooy If I use that one, the output will look like this ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']. I don't want it to show the [] and '' Commented Feb 13, 2018 at 5:17
  • @VyQuangDao, make sure you didn't miss the * Commented Feb 13, 2018 at 5:18

4 Answers 4

1

Before the print function is called, all of it's arguments are evaluated.

The second argument (outputlist(month)) has a side-effect. It prints all the month names. It just returns the very last one.

When the print function is called, it's just:

print("The list of the months of the year are:", "December")

but outputlist has already polluted your screen by then

Sign up to request clarification or add additional context in comments.

Comments

0

The print in your main function is printing what is returned by your outputlist function (i.e, the final list_output, December). The output list function itself is being called before the print because of python's flow of execution. The print function will just print the values you pass it. In order to know what the value is of the outputlist function, it has to call it. So first python calls outputlist, then it gets the return value of that and passes it to print.

Comments

0

If you want same code you can try this . In your code you are returning only last element of list to caller. And Also you are printing output in outputlist()

def outputlist(list):
    print("The list of the months of the year are:")
    for list_output in list:
       print(list_output,end=" ")
     #return list_output

def main():
    month = ["January", "February", "March", "April", "May", "June", "July", "August", "Setemper", "October", "November", "December"]
    outputlist(month)

main()

Comments

0

I think what you want is this

def outputlist(list):
    return_var = ""
    for list_output in list: # list_output is the element of the list we currently look at
       print(list_output,end=" ")
       return_var += list_output + " " # thus printed var is the line being printed
       # the loop is at it's end, the value of list_output is discarded, 
       # and overwritten by the next element
    return return_var # to return the printed text
    return list_output # to return the last value of list_output that has not been flushed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.