[Tutor] Positional Arguments

Alan Gauld alan.gauld at btinternet.com
Fri Nov 1 23:53:07 CET 2013


On 01/11/13 18:43, Jenny Allar wrote:

> but I'm having issues passing values from one function to another.

I'm not sure what you are asking. The error is pretty clear and the 
cause is as described - you don;t pass any arguments to the function...

> def main():
>      gethours()
>      print()
>      calcandprint()
>      calc_pay()


You don't pass any values to the functions, hence the error

> def calcandprint (hrswrkd, payrate):

But you define it to take two values in.

>      return hrswrkd, payrate

And it returns two values but you don't assign them
anywhere in your main() function.

> def calc_pay(hrswrkd, payrate):

Also expects two values.

>      return regpay, overtimepay, totalpay

And returns 3 values but again in main() you don't pass any values in 
and you don't do anything with the values returned.

Also as a general rule its better not to mix calculation and printing in 
a single function.
Better to pass in the data, calculate the results and pass those back to 
another function to print them. It makes the code easier to read
and easier to reuse in another program (such as a web app or GUI maybe)

> Traceback (most recent call last):
>    File "C:\", line 93, in <module>
>      main()
>    File "C:\", line 15, in main
>      calcandprint() # Call the function that calculates and prints the
> information
> TypeError: calcandprint() missing 2 required positional arguments:
> 'hrswrkd' and 'payrate'

It tells you the problem and your code clearly doesn't pass the values. 
So what part do you not understand?

Can we simplify the exercise a bit and just write a short function
that takes in a two arguments and returns a single value?
How about Pythagoras theorem? Write a function called hypotenuse
that takes two sides of a right angle triangle and returns
the hypotenuse? [ ie c = sqrt(a**2 + b**2) ]

And then write a main function that asks the user for the two values and 
prints the result? Can you do that just to check that you
understand the mechanics?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list