1

Pointers on how to fix my code? This code asks for user input on the number of tickets sold, and returns the income generated using functions

I am not sure about what how to call each function

secA = 20
secB = 15
secC = 10

def main():
    print("The income generated from all sections is: ", total)
def getTickets(A,B,C):
    sectionA = int(input("Please enter the number of tickets sold in section A: ")
    sectionB = int(input("Please enter the number of tickets sold in section B: ")
    sectionC = int(input("Please enter the number of tickets sold in section C: ")

    def ticketsValid():
        while sectionA > 300:
                print("ERROR: Section A has a limit of 300 seats")
        while sectionB > 500:
                print("ERROR: Section B has a limit of 500 seats")
        while sectionC > 200:
                print("ERROR: Section C has a limit of 200 seats")

    def calcIncome():
        total = secA * sectionA + secB * sectionB + secC * sectionC
        print("The income generated is $", format(total, '.2f'))   
main()
2
  • 1
    If you don't know how to deal with nested functions, then why are you nesting functions? What problem do you hope to solve by nesting the functions? Commented Mar 10, 2013 at 21:51
  • @karl I don't believe that that was the original name. I think that it was changed as that was thought to have been the purpose of the question. Commented Mar 11, 2013 at 1:37

2 Answers 2

3

To answer your first question: to call all of the functions you need to put the names of your functions into the main() function. But, you had several other errors so I have decided to walk you through the program, step-by-step.

First, we set the prices:

secA = 20
secB = 15
secC = 10

Here is the first function, getTickets()

def getTickets():

    global A
    A = int(input("Please enter the number of tickets sold in section A: "))

    global B
    B =int(input("Please enter the number of tickets sold in section B: "))

    global C
    C =int(input("Please enter the number of tickets sold in section C: "))

Notice the word global before I use the variable. This tells the computer that this variable can be used everywhere. Next, notice the double parentheses - since both int() and input() are functions, so we need to show that by doing that.

I fixed your code for the ticketsValid() function. Usually, it isn't a good idea to nest functions, so this is at the same indentation level as the above code.

def ticketsValid(A,B,C):
    while A > 300 or A < 0:
        print("ERROR: Section A has a limit of 300 seats\n")
        A = int(input("Please enter the number of tickets sold in section A: "))
    while B > 500 or B < 0:
        print("ERROR: Section B has a limit of 500 seats")
        B =int(input("Please enter the number of tickets sold in section B: "))
    while C > 200 or C < 0:
        print("ERROR: Section C has a limit of 200 seats")
        C =int(input("Please enter the number of tickets sold in section C: "))

This gets the variables from above, and checks to see if they are valid. Notice that I added a check for negative numbers - you can't sell negative tickets.

Then we come to calcIncome(A,B,C):

def calcIncome(A, B, C):
    total = A * secA + B * secB + C * secC
    print ("The income generated is $%d" % (total))

First, we multiply the sections by the set prices to calculate the total. Then, we print it.

Lastly, we need to call the functions. I used your idea for a main() function, which uses the other functions. It looks like this.

def main():
    getTickets()
    ticketsValid(A,B,C)
    calcIncome(A, B, C)

It simply calls the other functions, in the correct order, when run.

Lastly, we call the main() function by typing:

main()

I hope that this answered your question. If not, feel free to comment. If so, please check the green check mark next to my answer.

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you so much! I understand the errors I made on this problem. However, my teacher gave us certain parameters on this problem. She does want us to include the input function "getTickets." as well as a Boolean indicating whether the passed tickets sold for the given section is in the valid range. If the number of tickets exceeds the limit, I suppose she wants the program to ask again for a valid number. If you could give me pointers on how to create this program with those parameters that would be great. This is my first programming class I have ever taken.
@KevinChoi Ok - thanks for the clarification. I will update my code with the proper stuff (for lack of a better word.) Also, I recommend that you buy Python for the Absolute Beginner which will help you get ahead in class. It's an awesome book.
@KevinChoi Also, do you have to nest the functions? That is way more complex. Lastly, is it ok to merge the getTickets() and ticketsValid(A,B,C) functions?
Nesting functions is not required, though I think she does want to have those three functions on their own. She wants the input function "getTickets" and validation function "ticketsValid" on their own.
@KevinChoi I fixed my answer. Hopefully this is satisfactory. If you need any help, go to chat and say .@xxmbabanexx <insert myMessage/Link to question> I will try to help you as quickly as possible.
|
1

If you only want to know how to use a nested function:

def f():
    def g(): #this defines a function but does not call it
        print "run g"
    g() # this calls g.

Typically, nested functions should not be available outside its parent function. Since the point of using a nested function is that the function only helps its parent function to do things. If you want it outside, consider define it as a new function.

In your case, you need to consider how to break up your code into parts.
If I were you, I would use getTickets() to get tickets.
The ticketsValid is fine, though I would let it return a boolean. calcIncome would return the total income. So the general design is like:

def main():
    (A, B, C) = getTickets()
    if(ticketsValid(A, B, C)):
        income = calcIncome(A, B, C)
        print("The income generated from all sections is: ", income)

def getTickets():......
def ticketsValid(A, B, C):......
def calcIncome(A, B, C):......

I think this would be a better design.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.