0

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.

  • You might want to learn about pass by value vs. pass by reference. The other thing that could be confusing you is that names are only bound in the scope in which they're defined. It doesn't matter than you named it `subtotal` in both functions, it's a completely separate value in `appetizers(subtotal):` - nothing would be different if you changed that to `appetizers(fido)` and changed all occurences of `subtotal` to `fido` within the scope of `appetizers`. – Edward Peters Nov 26 '22 at 03:17
  • 1
    I'd really suggest scaling down to the bare minimum - two or three functions in an online python interpreter - and playing around with passing, changing and printing variables until you understand it inside and out. Trying to learn the basic concepts on code that's even this complex is burdening yourself with a lot of moving parts. – Edward Peters Nov 26 '22 at 03:19
  • Thank you for your help. Honestly, what you said went all over my head now. I will definitely try doing that. – goatishnewcastle49 Nov 26 '22 at 03:28
  • @EdwardPeters python uses *neither one* of those evaluation strategies. – juanpa.arrivillaga Nov 26 '22 at 04:23
  • @juanpa.arrivillaga It's called pass by assignment, but the behavior is what's classically described as pass by value - see https://stackoverflow.com/questions/29776736/python-and-java-parameter-passing . I'm not sure why Python describes itself differently, except possibly in an attempt to avoid the sort of confusion people always have over Java? – Edward Peters Nov 26 '22 at 13:33
  • @juanpa.arrivillaga That wasn't rhetorical, btw, I'm genuinely wondering and would appreciate insight on why they're described differently. I didn't really see an answer to that in the linked question. – Edward Peters Nov 26 '22 at 13:37

1 Answers1

1

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)
Dash
  • 1,191
  • 7
  • 19
  • Remember to add a `return subtotal` to `appetizers`! – Samwise Nov 26 '22 at 03:27
  • @Samwise I believe menu option 4 returns `subtotal` – Dash Nov 26 '22 at 03:29
  • @Dash should I replace ```appetizers(subtotal)``` with that code or add it as a separate line? Could that be also used to actually call the appetizers function? – goatishnewcastle49 Nov 26 '22 at 03:33
  • @goatishnewcastle49 `appetizers(subtotal)` is already calling the appetizers function, I'm saying you need to save the `appetizers` return value by assigning the function call to a variable. (In this case, `subtotal`) – Dash Nov 26 '22 at 03:36
  • oh I see -- hard to tell where the loops begin and end with that tiny indentation (: – Samwise Nov 26 '22 at 15:53