Okay, is it possible to return two variables in one function?
Returning two variables?
Collapse
X
-
-
before barton comes, i wish to say, pls use code tags for the code. :)Originally posted by parthpatelYes 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 tupleComment
-
Excellent reply, parthpatel.Originally posted by parthpatelYes it is possible to return two variables from a single function
it will return (1,2) as a tupleCode:def A(self): z= 1 c= 2 return z,c
But I must point out that this syntax is for a class method and sould include the class header:
And would be called as:Code:class A_Class def A(self): z= 1 c= 2 return z,cFor a function use:Code:anInstance = A_Class() print anInstance.A()
Next time, there is a panel on the right and side called "REPLY GUIDELINES" that shows what CODE tags look like.Code:def A(): z = 1 c = 2 return z, c print A() ## OR ## a, b = A() print a, b
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
-
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
-
You are very close. You need to assign funcion results to variables, then pass them to the next function:Originally posted by shycat518Since 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()
I threw in the test for a case where hours are less that 40.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()
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" % otpayComment
Comment