Returning two variables?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shycat518
    New Member
    • Mar 2007
    • 11

    #1

    Returning two variables?

    Okay, is it possible to return two variables in one function?
  • parthpatel
    New Member
    • Apr 2007
    • 14

    #2
    Yes it is possible to return two variables from a single function
    Code:
    def A(self):
        z= 1
        c=  2
        return z,c
    it will return (1,2) as a tuple
    Last edited by bartonc; Apr 11 '07, 08:10 AM. Reason: added [code][/code] tags

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by parthpatel
      Yes it is possible to return two variables from a single function

      def A(self):
      z= 1
      c= 2
      return z,c

      it will return (1,2) as a tuple
      before barton comes, i wish to say, pls use code tags for the code. :)

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by parthpatel
        Yes it is possible to return two variables from a single function
        Code:
        def A(self):
            z= 1
            c=  2
            return z,c
        it will return (1,2) as a tuple
        Excellent reply, parthpatel.

        But I must point out that this syntax is for a class method and sould include the class header:
        Code:
        class A_Class
            def A(self):
                z= 1
                c=  2
                return z,c
        And would be called as:
        Code:
        anInstance = A_Class()
        print anInstance.A()
        For a function use:
        Code:
        def A():
            z = 1
            c =  2
            return z, c
        
        print A()
        ## OR ##
        a, b = A()
        print a, b
        Next time, there is a panel on the right and side called "REPLY GUIDELINES" that shows what CODE tags look like.

        Your editor has a # button that is very handy for this. It works best with the "Enhanced Mode" radio button selected.

        Thanks for helping out in the Python Forum on TheScripts.com.

        Comment

        • shycat518
          New Member
          • Mar 2007
          • 11

          #5
          Awesome, thanks. :)

          Comment

          • shycat518
            New Member
            • Mar 2007
            • 11

            #6
            That's good to know I was at least doing it half right. :) I didn't try the ().

            Comment

            • shycat518
              New Member
              • Mar 2007
              • 11

              #7
              Since I'm still new at Python and am having tons of problems with more than one functions, I've been playing around with them.

              Here's program I've been working with. It, of course, doesn't work. According to my boyfriend I still don't understand passing and returning variables. :P At least I'm trying. . .

              Code:
              def main():
                  askfor()
                  hours()
                  pay()
              
              def askfor():    
                  hours = input("How many hours were worked? ")
                  rate = input("What is the pay rate? ")
                  print "Pay rate = $", rate
                  return (hours,rate)
              
              def hours():
                  reghr = 40
                  print "Regular Hours = ", reghr
                  overtime = hours - 40
                  print "Overtime hours ", overtime
                  return (reghr, overtime)
              
              def pay(rpay, otpay):
                  pay = (40 * rate)
                  print "Regular Pay = $",pay
                  otpay = (1.5 * rate)*(overtime) 
                  print "Overtime Pay = $", otpay
                  total = pay + otpay
                  print "Total Pay = $", total 
                  return (pay, total)
              
              main()

              Here's the same program using one funtion that works. . . I want the same output as this one but with the 4 function one. You would think that since I can get everything to work on the one function program, I could divide it up easier.

              Code:
              def main():
                  hours = input("How many hours were worked? ")
                  rate = input("What is the pay rate? ")
                  print "Pay rate = $", rate
                  reghr = 40
                  print "Regular Hours = ", reghr
                  overtime = hours - 40
                  print "Overtime hours ", overtime
                  pay = (40 * rate)
                  print "Regular Pay = $",pay
                  otpay = (1.5 * rate)*(overtime) 
                  print "Overtime Pay = $", otpay
                  total = pay + otpay
                  print "Total Pay = $", total 
              main()

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by shycat518
                Since I'm still new at Python and am having tons of problems with more than one functions, I've been playing around with them.

                Here's program I've been working with. It, of course, doesn't work. According to my boyfriend I still don't understand passing and returning variables. :P At least I'm trying. . .

                Code:
                def main():
                    askfor()
                    hours()
                    pay()
                
                def askfor():    
                    hours = input("How many hours were worked? ")
                    rate = input("What is the pay rate? ")
                    print "Pay rate = $", rate
                    return (hours,rate)
                
                def hours():
                    reghr = 40
                    print "Regular Hours = ", reghr
                    overtime = hours - 40
                    print "Overtime hours ", overtime
                    return (reghr, overtime)
                
                def pay(rpay, otpay):
                    pay = (40 * rate)
                    print "Regular Pay = $",pay
                    otpay = (1.5 * rate)*(overtime) 
                    print "Overtime Pay = $", otpay
                    total = pay + otpay
                    print "Total Pay = $", total 
                    return (pay, total)
                
                main()

                Here's the same program using one funtion that works. . . I want the same output as this one but with the 4 function one. You would think that since I can get everything to work on the one function program, I could divide it up easier.

                Code:
                def main():
                    hours = input("How many hours were worked? ")
                    rate = input("What is the pay rate? ")
                    print "Pay rate = $", rate
                    reghr = 40
                    print "Regular Hours = ", reghr
                    overtime = hours - 40
                    print "Overtime hours ", overtime
                    pay = (40 * rate)
                    print "Regular Pay = $",pay
                    otpay = (1.5 * rate)*(overtime) 
                    print "Overtime Pay = $", otpay
                    total = pay + otpay
                    print "Total Pay = $", total 
                main()
                You are very close. You need to assign funcion results to variables, then pass them to the next function:
                Code:
                def main():
                    h, r = askfor()
                    reg, ot  = hours(h)
                    pay(r, reg, ot)
                
                def askfor():    
                    hours = input("How many hours were worked? ")
                    rate = input("What is the pay rate? ")
                    print "Pay rate = $", rate
                    return (hours,rate)
                
                def hours(hours):
                    overtime = 0
                    if hours > 40:
                        overtime = hours - 40
                        hours = 40
                    print "Regular Hours = ", hours
                    print "Overtime hours ", overtime
                    return (hours, overtime)
                
                def pay(rate, reg, overtime):
                    pay = (reg * rate)
                    print "Regular Pay = $",pay
                    otpay = (1.5 * rate)*(overtime) 
                    print "Overtime Pay = $", otpay
                    total = pay + otpay
                    print "Total Pay = $", total 
                    return (pay, total)
                
                main()
                I threw in the test for a case where hours are less that 40.
                Also, your use of print adds a space between '$' and the variable evaluation.
                Here are a couple of ways to get rid of that:
                Code:
                    print "Overtime Pay = $" + str(otpay)
                # or (more advanced, but worth learning)
                    print "Overtime Pay = $%.2f" % otpay

                Comment

                Working...