Home > Enterprise >  Trouble with using the function feature in python
Trouble with using the function feature in python

Time:10-24

I am trying to complete the function "CalcServiceFee" I need the following info,

Ticket Price: Service Fee: Grand Total:

the ticket prices are (38.00, 42.00, and 24.00) the service fee is 0.07 and the grand total is just the ticket price * 0.07

The problem is that I'm not sure how to properly use the argument feature, as I am supposed to create an argument for the CalcServiceFee function named "subtotal". I'd really appreciate any and all advice.

 def DisplayOptions():
        print("--------------------------------------------------------")
        print("Ticket Options")
        print("---------------------------------------------------------")
        print("Weekday                          $38.00")
        print("Weekend                          $42.00")
        print("Twilight                         $24.00")
        print()
        enter = input("Press enter to return to menu: ")
        if enter == "":
            main()
    
        
    
    def CalculatePrice():
        print("--------------------------------------------------------------")
        print("Ticket Price Calculator")
        print("---------------------------------------------------------------")
        print()
        TicketOption = input("Enter ticket option: (1 - Weekday, 2 - Weekend, 3 - Twilight)")
        if TicketOption == "1":
            loop = False
            print("Ticket Price:     $38.00")
            print("Service Fee: "   CalcServiceFee
    
    
    
    
    def CalcServiceFee(ServiceFee):
        



def main():
    strChoice = ""
    loop = True
    while loop:
        print("Menu of options")
        print(" -------------------------------------------------")
        print("C: Calculate Ticket Cost")
        print("D: Display Ticket Options")
        print("X: Exit Application")
        print()
        strChoice = input("Enter your menu selection: ").upper()
        print()
        if strChoice == "X":
            print("exiting menu")
            loop = False
        if strChoice == "D":
            DisplayOptions()
        if strChoice == "C":
            CalculatePrice()
main()

CodePudding user response:

I think you want something like this.

print("Service Fee: "   str(CalcServiceFee(38))

def CalcServiceFee(TicketPrice):
    return TicketPrice * 0.07

CodePudding user response:

Testing out your code, there were some bits missing that needed to be added to produce an executable program. Viewing the intent of your program, following is a refined version of your code.

fee = .08   # A variable assignment of a fee percentage for an example

def DisplayOptions():
    print("--------------------------------------------------------")
    print("Ticket Options")
    print("---------------------------------------------------------")
    print("Weekday                          $38.00")
    print("Weekend                          $42.00")
    print("Twilight                         $24.00")
    print()
    enter = input("Press enter to return to menu: ")
    return      # Removed the call to main - no reason noted to have main a recursive function call
    
def CalculatePrice():
    while True:
        print("--------------------------------------------------------------")
        print("Ticket Price Calculator")
        print("---------------------------------------------------------------")
        print()
        TicketOption = input("Enter ticket option: (1 - Weekday, 2 - Weekend, 3 - Twilight)")
        if TicketOption == "1":
            print("Ticket Price:     $38.00")
            print("Service Fee:", format(CalcServiceFee(38.00), '.2f'))
        if TicketOption == "2":
            print("Ticket Price:     $42.00")
            print("Service Fee:", format(CalcServiceFee(42.00), '.2f'))
        if TicketOption == "3":
            print("Ticket Price:     $24.00")
            print("Service Fee:", format(CalcServiceFee(24.00), '.2f'))
        if TicketOption == "1" or TicketOption == "2" or TicketOption == "3":
            break
        
def CalcServiceFee(ServiceFee):
    return ServiceFee * fee

def main():
    strChoice = ""
    
    while True:             # Use Boolean constant with "break" to simplify things
        print("Menu of options")
        print("-------------------------------------------------")
        print("C: Calculate Ticket Cost")
        print("D: Display Ticket Options")
        print("X: Exit Application")
        print()
        strChoice = input("Enter your menu selection: ").upper()
        print()
        if strChoice == "X":
            print("exiting menu")
            break
        if strChoice == "D":
            DisplayOptions()
        if strChoice == "C":
            CalculatePrice()
            
main()

Here are a few points to note.

  • In your code, the main function was being called from the DisplayOption function. That would would result in a recursive call which would complicate things a bit when you finally want to end the program. So, that was changed out with a simple return to the main function instead.
  • Not knowing if a simple percentage would be applied versus some type of tiered fee structure, a simple one-off percentage was added for calculation and testing purposes.
  • Instead of controlling the while loops with a Boolean variable that would get modified, the "True" Boolean constant along with a break statement was used to simplify things.

With that, the program was run utilizing some sample choices.

@Dev:~/Python_Programs/ServiceFee$ python3 Service.py 
Menu of options
-------------------------------------------------
C: Calculate Ticket Cost
D: Display Ticket Options
X: Exit Application

Enter your menu selection: c

--------------------------------------------------------------
Ticket Price Calculator
---------------------------------------------------------------

Enter ticket option: (1 - Weekday, 2 - Weekend, 3 - Twilight)3
Ticket Price:     $24.00
Service Fee: 1.92
Menu of options
-------------------------------------------------
C: Calculate Ticket Cost
D: Display Ticket Options
X: Exit Application

Enter your menu selection: 

Give that a try and see if that meets the spirit of your project.

  • Related