Python Forum
trouble with functions "def", calling/defining them
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trouble with functions "def", calling/defining them
#1
Question 
please suggest the right forum or site if this is not the correct place, thank you!

Hello all, I am currently having trouble understanding an error I receive when running my code
error:
Traceback (most recent call last):
  File "main.py", line 32, in <module>
    showIncome()
TypeError: showIncome() missing 6 required positional arguments: 'num_aTickets', 'num_bTickets', 'num_cTickets', 'aRevenue', 'bRevenue', and 'cRevenue'
My code with comments/notes: the error happens in the end when calling showIncome()" I am having trouble within "def showIncome():" as well as calling/figuring out the a/b/cRevenue variables in the print statements. any help or resources explaining this will be greatly beneficial as my teacher and book do not seem to be helping much ( i have tried stackoverflow but reached my question limit this week and am resorting to this forum)
# Your main function should get the number of seats sold for each category. The values should be sent to a calcIncome function.
# The calcIncome function should calculate the income from each category. Those values should be returned to the main function and those values should be sent to the showIncome function.
# The showIncome function should calculate the total income generated from the ticket sales, display the income generated from each category AND display the total income generated.

def main():
    # obtain number of tickets/seats for each type from user
    num_aTickets = int(input('Enter number of class a tickets sold: '))
    num_bTickets = int(input('Enter number of class b tickets sold: '))
    num_cTickets = int(input('Enter number of class c tickets sold: '))
    return num_aTickets, num_bTickets, num_cTickets 


def calcIncome(aTickets, bTickets, cTickets):
      # revenue made from each and returns total revenue from sales
    aCost = 20
    bCost = 15
    cCost = 10
    aRevenue = aTickets * aCost
    bRevenue = bTickets * bCost
    cRevenue = cTickets * cCost
    return (aRevenue + bRevenue + cRevenue)


def showIncome(num_aTickets, num_bTickets, num_cTickets, aRevenue, bRevenue, cRevenue):
    income = calcIncome(num_aTickets, num_bTickets, num_cTickets)
    print('the revenue collected from total ticket sales is', income)
    print('the revenue collected from class a ticket sales is', aRevenue)
    print('the revenue collected from class b ticket sales is', bRevenue)
    print('the revenue collected from class c ticket sales is', cRevenue)


showIncome()
Reply
#2
A function can be created to take some number of arguments. If so, they have to be passed in when called.

def myfunc(alpha, beta): # This function requires 2 arguments be passed in
    print(f"Alpha is set to {alpha} and Beta is set {beta}")


myfunc(7, 3)  # called with 2 arguments: a 7 and a 3
myfunc()      # called with no arguments
And when run, we see the first time it works and the second time it fails

Output:
Alpha is set to 7 and Beta is set 3 Traceback (most recent call last): File "funcinfo.py", line 6, in <module> myfunc() # called with no arguments TypeError: myfunc() missing 2 required positional arguments: 'alpha' and 'beta'
You have created a function showIncome that takes six arguments. But you have called the function with no arguments, so it doesn't know what to assign to those six variables. It want to know how many tickets there were. Where should it find that information?
Reply
#3
(Oct-21-2020, 01:22 AM)bowlofred Wrote: A function can be created to take some number of arguments. If so, they have to be passed in when called.

I don't they I am fully comprehending this. I have all 6 of the arguments used (the num_a/b/cTickets and a/b/cRevenue)inside the "def showIncome(num_aTickets, num_bTickets, num_cTickets, aRevenue, bRevenue, cRevenue):" area...
my last line is calling the showIncome "def" to display the print functions and do calulations at least that how it is working in my head.
Reply
#4
But line 32 above is where you call it, and there you have not supplied any arguments. One possible call might be:

showIncome(2, 5, 12, 100, 200, 500)
This means when showIncome() is run, num_aTickets is set to 2, num_bTickets is set to 5, etc. Without those values passed in, the function doesn't know what they should be set to.
Reply
#5
(Oct-21-2020, 02:12 AM)bowlofred Wrote: But line 32 above is where you call it, and there you have not supplied any arguments.

This did not work, I tried the numbers and using a variable such as a/b/cRevenue (like in the code) with the same error.
I think the issue is surrounding the aRevenue, bRevenue, cRevenue variables in the def showIncome(...): section but I can not figure out a fix to save my life.
Reply
#6
"did not work" doesn't say much. Your first error was that the function wasn't being called with 6 arguments. If you called it with 6 arguments, that error should go away. That might allow it to go forward and find another error which you'll have to deal with.

Can you show the changes you made and the exact error that you're getting now?
Reply
#7
forgot to mention the change... (last def the a/b/cRevenue variables are not defined)
def main():
    # obtain number of tickets/seats for each type from user
    num_aTickets = int(input('Enter number of class a tickets sold: '))
    num_bTickets = int(input('Enter number of class b tickets sold: '))
    num_cTickets = int(input('Enter number of class c tickets sold: '))
    return num_aTickets, num_bTickets, num_cTickets 


def calcIncome(aTickets, bTickets, cTickets):
      # revenue made from each and returns total revenue from sales
    aCost = 20
    bCost = 15
    cCost = 10
    aRevenue = aTickets * aCost
    bRevenue = bTickets * bCost
    cRevenue = cTickets * cCost
    return (aRevenue + bRevenue + cRevenue)


def showIncome(num_aTickets, num_bTickets, num_cTickets):
    income = calcIncome(num_aTickets, num_bTickets, num_cTickets)
    print('the revenue collected from total ticket sales is', income)
    print('the revenue collected from class a ticket sales is', calcIncome(aRevenue))
    print('the revenue collected from class b ticket sales is', calcIncome(bRevenue))
    print('the revenue collected from class c ticket sales is', calcIncome(cRevenue))


showIncome(1, 2, 3)
... and here is the error I get as of now after trying your first comment (with the numbers)

the revenue collected from total ticket sales is 80
Traceback (most recent call last):
  File "main.py", line 32, in <module>
    showIncome(1, 2, 3)
  File "main.py", line 27, in showIncome
    print('the revenue collected from class a ticket sales is', calcIncome(aRevenue))
NameError: name 'aRevenue' is not defined
 
Reply
#8
Variables created in a function normally have scope only in that particular function. So creating one in calcIncome() doesn't make it visible in showIncome()

In showIncome(), aRevenue isn't assigned. So when it's used on line 23, it doesn't know what the value should be. It needs to be set to something, or it needs to not be used.
Reply
#9
(Oct-21-2020, 02:51 AM)bowlofred Wrote: In showIncome(), aRevenue isn't assigned. So when it's used on line 23, it doesn't know what the value should be. It needs to be set to something, or it needs to not be used.

I see now, how would I go about setting that value? I tried making a variable named as aRevenue but how would i get it to call the info i need from the calcIncome?

def showIncome(num_aTickets, num_bTickets, num_cTickets):
    aRevenue = calcIncome()
    bRevenue = calcIncome()
    cRevenue = calcIncome()
    income = calcIncome(num_aTickets, num_bTickets, num_cTickets)
    print('the revenue collected from total ticket sales is', income)
    print('the revenue collected from class a ticket sales is', aRevenue)
    print('the revenue collected from class b ticket sales is', bRevenue)
    print('the revenue collected from class c ticket sales is', cRevenue)


showIncome(1, 2, 3)
I tried the above by placing it in the showIncome but that came back as
Error:
Traceback (most recent call last): File "main.py", line 35, in <module> showIncome(1, 2, 3) File "main.py", line 25, in showIncome aRevenue = calcIncome() TypeError: calcIncome() missing 3 required positional arguments: 'aTickets', 'bTickets', and 'cTickets'
Reply
#10
But you could pass it in to the function. Place it in the argument list and then put the data in when it is called. Or, have it ask another function for the data. But since the caller doesn't have the data either, this doesn't seem the best.

Right now, calcIncome() returns the sum of all the revenue. It could instead return all the revenue data. (It could do that in a dict). If so, other functions could make use of that data (say printing them out).

So some better choices:
* have showIncome() call calcIncome(), and have calcIncome() return all the revenues.
* Don't print in showIncome() since it doesn't readily have the revenue. Print where the data is already present.
* have whatever calls showIncome() pass the revenue data in (that would require more changes and seems a poor way to do it)
* make the revenue a global variable (starts bad habits that make things more complicated as they get big)


Also, you have a function called main(), but nothing seems to call it. Is that intentional?
Duck_Boom likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 747 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,643 Dec-09-2021, 12:40 PM
Last Post: deanhystad
  Defining Functions theturtleking 4 2,733 Dec-07-2021, 06:45 PM
Last Post: deanhystad
  Defining multiple functions in the same def process sparkt 5 2,753 Aug-09-2020, 06:19 PM
Last Post: sparkt
  Calling C functions with PyObjects jibarra 6 2,655 Jul-17-2019, 02:52 PM
Last Post: jibarra
  Defining functions TommyAutomagically 1 1,841 Apr-25-2019, 06:33 PM
Last Post: Yoriz
  calling os functions not in module os Skaperen 2 2,588 Nov-10-2018, 01:54 AM
Last Post: Skaperen
  Trouble calling functions in main function RedSkeleton007 6 4,992 Nov-11-2017, 01:22 PM
Last Post: sparkz_alot
  Calling functions from another file jp2017 11 30,404 Oct-11-2017, 10:58 PM
Last Post: snippsat
  Having trouble defining variable tannishpage 6 5,564 Mar-23-2017, 01:04 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020