How can I return two values from a function in Python?

前端 未结 8 825
执笔经年
执笔经年 2020-11-22 12:14

I would like to return two values from a function in two separate variables. For example:

def select_choice():
    loop = 1
    row = 0
    while loop == 1:         


        
8条回答
  •  无人共我
    2020-11-22 12:37

    You can return more than one value using list also. Check the code below

    def newFn():    #your function
      result = []    #defining blank list which is to be return
      r1 = 'return1'    #first value
      r2 = 'return2'    #second value
      result.append(r1)    #adding first value in list
      result.append(r2)    #adding second value in list
      return result    #returning your list
    
    ret_val1 = newFn()[1]    #you can get any desired result from it
    print ret_val1    #print/manipulate your your result
    

提交回复
热议问题