Skip to content
Advertisement

I cannot pass a parameter inside a function to another function (Python)

I successfully defined a parameter and passed it to a function but when I return to main menu that parameter’s value is completely reset and I cannot use it anywhere. The value of the parameter stays only within the second function. Like, the parameter cannot communicate with the whole program as far as I understand.

def main_menu(subtotal):
  while True:
    print("1) Appetizers")
    print("2) Option 2")
    print("3) Option 3")
    print("4) Option 4")
    print("5) Option 5")
    print(" ")
    print("Current overall subtotal: $" + str(round(subtotal,2)))
 
    while True:
      try:
        choice = int(input("What is your choice? "))
        break
      except ValueError:
        print("Please enter whole numbers only.")


    while choice > 5 or choice < 1:
      print("Please choose an option from 1 to 5")
      try:
        choice = int(input("what is your choice? "))
      except ValueError:
        print("Please enter whole numbers only.")

    if choice == 1:
      appetizers(subtotal)
    """
    elif choice == 2:
      option_2(subtotal)
    elif choice == 3:
      option_3(subtotal)
    elif choice == 4:
      option_4(subtotal)
    elif choice == 5:
      end(subtotal)
      return subtotal
    """

def appetizers(subtotal):
  while True:
    print("1) Option 1")
    print("2) Option 2")
    print("3) Option 3")
    print("4) Return to Main Menu")
    print(" ")
    print("Current overall subtotal: $" + str(round(subtotal,2)))
   
    product_amount = 1

    while True:
      try:
        choice = int(input("What is your choice? "))
        break
      except ValueError:
        print("Please enter whole numbers only.")

    while choice > 4 or choice < 1:
      print("Please choose an option from 1 to 4")
      try:
        choice = int(input("what is your choice? "))
      except ValueError:
        print("Please enter whole numbers only.")

    if choice == 4:
      return subtotal
    else:
      while True:
        try:
          product_amount = int(input("How many would you like? "))
          break
        except ValueError:
          print("Please enter whole numbers only.")

    while product_amount > 100000 or product_amount < 1:
      print("Please choose an option from 1 to 100,000")
      product_amount = int(input("How many would you like? "))

    if choice == 1:
      subtotal = subtotal + (product_amount * 4.99)
    elif choice == 2:
      subtotal = subtotal + (product_amount * 2.99)
    elif choice == 3:
      subtotal = subtotal + (product_amount * 8.99)

For this project’s sake, I don’t want to use global variables. I only want to use the subtotal variable as a parameter throughout the program and continuously alter its value throughout the program and make calculations with it. I want to do this by passing it through other functions.

Advertisement

Answer

Since you’ve written the operations into functions and you’re passing in the current subtotal, you just need to be updating the subtotal by saving the return value from appetizers() in main_menu(), like here:

# ...
if choice == 1:
      subtotal = appetizers(subtotal)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement