Okay, is it possible to return two variables in one function?
7 15539
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
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. :)
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
Excellent reply, parthpatel.
But I must point out that this syntax is for a class method and sould include the class header: - class A_Class
-
def A(self):
-
z= 1
-
c= 2
-
return z,c
And would be called as: - anInstance = A_Class()
-
print anInstance.A()
For a function use: - 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.
That's good to know I was at least doing it half right. :) I didn't try the ().
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. . . -
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. -
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()
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. . . -
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. -
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: -
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: -
print "Overtime Pay = $" + str(otpay)
-
# or (more advanced, but worth learning)
-
print "Overtime Pay = $%.2f" % otpay
Sign in to post your reply or Sign up for a free account.
Similar topics
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 :...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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
//...
|
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()...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |