How is returning the output of a function different from printing it?

后端 未结 7 1703
死守一世寂寞
死守一世寂寞 2020-11-21 11:23

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 functio

7条回答
  •  爱一瞬间的悲伤
    2020-11-21 12:19

    you just add 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
      return parts_dict
    

    printing out only prints out to the standard output (screen) of the application. You can also return multiple things by separating them with commas:

    return parts_dict, list_of_parts
    

    to use it:

    test_dict = {}
    test_dict = autoparts()
    

提交回复
热议问题