473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Returning two variables?

Okay, is it possible to return two variables in one function?
Apr 11 '07 #1
7 15539
Yes it is possible to return two variables from a single function
Expand|Select|Wrap|Line Numbers
  1. def A(self):
  2.     z= 1
  3.     c=  2
  4.     return z,c 
it will return (1,2) as a tuple
Apr 11 '07 #2
ghostdog74
511 Expert 256MB
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. :)
Apr 11 '07 #3
bartonc
6,596 Expert 4TB
Yes it is possible to return two variables from a single function
Expand|Select|Wrap|Line Numbers
  1. def A(self):
  2.     z= 1
  3.     c=  2
  4.     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:
Expand|Select|Wrap|Line Numbers
  1. class A_Class
  2.     def A(self):
  3.         z= 1
  4.         c=  2
  5.         return z,c 
And would be called as:
Expand|Select|Wrap|Line Numbers
  1. anInstance = A_Class()
  2. print anInstance.A()
For a function use:
Expand|Select|Wrap|Line Numbers
  1. def A():
  2.     z = 1
  3.     c =  2
  4.     return z, c
  5.  
  6. print A()
  7. ## OR ##
  8. a, b = A()
  9. 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.
Apr 11 '07 #4
Awesome, thanks. :)
Apr 11 '07 #5
That's good to know I was at least doing it half right. :) I didn't try the ().
Apr 11 '07 #6
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. . .

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     askfor()
  3.     hours()
  4.     pay()
  5.  
  6. def askfor():    
  7.     hours = input("How many hours were worked? ")
  8.     rate = input("What is the pay rate? ")
  9.     print "Pay rate = $", rate
  10.     return (hours,rate)
  11.  
  12. def hours():
  13.     reghr = 40
  14.     print "Regular Hours = ", reghr
  15.     overtime = hours - 40
  16.     print "Overtime hours ", overtime
  17.     return (reghr, overtime)
  18.  
  19. def pay(rpay, otpay):
  20.     pay = (40 * rate)
  21.     print "Regular Pay = $",pay
  22.     otpay = (1.5 * rate)*(overtime) 
  23.     print "Overtime Pay = $", otpay
  24.     total = pay + otpay
  25.     print "Total Pay = $", total 
  26.     return (pay, total)
  27.  
  28. 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.

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     hours = input("How many hours were worked? ")
  3.     rate = input("What is the pay rate? ")
  4.     print "Pay rate = $", rate
  5.     reghr = 40
  6.     print "Regular Hours = ", reghr
  7.     overtime = hours - 40
  8.     print "Overtime hours ", overtime
  9.     pay = (40 * rate)
  10.     print "Regular Pay = $",pay
  11.     otpay = (1.5 * rate)*(overtime) 
  12.     print "Overtime Pay = $", otpay
  13.     total = pay + otpay
  14.     print "Total Pay = $", total 
  15. main()
Apr 12 '07 #7
bartonc
6,596 Expert 4TB
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. . .

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     askfor()
  3.     hours()
  4.     pay()
  5.  
  6. def askfor():    
  7.     hours = input("How many hours were worked? ")
  8.     rate = input("What is the pay rate? ")
  9.     print "Pay rate = $", rate
  10.     return (hours,rate)
  11.  
  12. def hours():
  13.     reghr = 40
  14.     print "Regular Hours = ", reghr
  15.     overtime = hours - 40
  16.     print "Overtime hours ", overtime
  17.     return (reghr, overtime)
  18.  
  19. def pay(rpay, otpay):
  20.     pay = (40 * rate)
  21.     print "Regular Pay = $",pay
  22.     otpay = (1.5 * rate)*(overtime) 
  23.     print "Overtime Pay = $", otpay
  24.     total = pay + otpay
  25.     print "Total Pay = $", total 
  26.     return (pay, total)
  27.  
  28. 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.

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     hours = input("How many hours were worked? ")
  3.     rate = input("What is the pay rate? ")
  4.     print "Pay rate = $", rate
  5.     reghr = 40
  6.     print "Regular Hours = ", reghr
  7.     overtime = hours - 40
  8.     print "Overtime hours ", overtime
  9.     pay = (40 * rate)
  10.     print "Regular Pay = $",pay
  11.     otpay = (1.5 * rate)*(overtime) 
  12.     print "Overtime Pay = $", otpay
  13.     total = pay + otpay
  14.     print "Total Pay = $", total 
  15. main()
You are very close. You need to assign funcion results to variables, then pass them to the next function:
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     h, r = askfor()
  3.     reg, ot  = hours(h)
  4.     pay(r, reg, ot)
  5.  
  6. def askfor():    
  7.     hours = input("How many hours were worked? ")
  8.     rate = input("What is the pay rate? ")
  9.     print "Pay rate = $", rate
  10.     return (hours,rate)
  11.  
  12. def hours(hours):
  13.     overtime = 0
  14.     if hours > 40:
  15.         overtime = hours - 40
  16.         hours = 40
  17.     print "Regular Hours = ", hours
  18.     print "Overtime hours ", overtime
  19.     return (hours, overtime)
  20.  
  21. def pay(rate, reg, overtime):
  22.     pay = (reg * rate)
  23.     print "Regular Pay = $",pay
  24.     otpay = (1.5 * rate)*(overtime) 
  25.     print "Overtime Pay = $", otpay
  26.     total = pay + otpay
  27.     print "Total Pay = $", total 
  28.     return (pay, total)
  29.  
  30. 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:
Expand|Select|Wrap|Line Numbers
  1.     print "Overtime Pay = $" + str(otpay)
  2. # or (more advanced, but worth learning)
  3.     print "Overtime Pay = $%.2f" % otpay
Apr 12 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Dr John Stockton | last post by:
What are the best ways of returning multiple results from a subroutine ? I've been using ... return } which is inelegant. I'm used to Pascal's procedure X(const A, B : integer; var C, D :...
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
8
by: briforge | last post by:
I am writing a program for the Palm OS in c++. I have already posted this to a palm development forum but I'm desperate for a fix, so I hope this cross-post isn't offensive. My program is...
10
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and...
2
by: Billy Cormic | last post by:
Is there a way to launch a new form and have the new form return a variable back to the parent form? Can this be done without the use of global variables? Thanks, Billy
6
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
2
by: Andy B | last post by:
I need to make a class and not quite sure how to go about doing this part. I want the class to take user input, build a dataset based on that input and then return it from the class so it can be...
6
Odisey
by: Odisey | last post by:
What is wring with this? <?php #Color instrument vars # ODC August 4, 2008 require_once ('??????.php'); //Connect to database RESET TO SAFE PATH ONCE IT FUNCTIONS //...
7
by: Ryan Le Piane | last post by:
Hi everyone, I am trying to program a Form Pay Estimator and it calculates the gross pay, taxes owed, and net pay for an individual employee. For some reason my CalculateTaxes method in my Pay()...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.