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.

6
  • 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. Commented Nov 26, 2022 at 3: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. Commented Nov 26, 2022 at 3:19
  • Thank you for your help. Honestly, what you said went all over my head now. I will definitely try doing that. Commented Nov 26, 2022 at 3:28
  • @EdwardPeters python uses neither one of those evaluation strategies. Commented Nov 26, 2022 at 4:23
  • @juanpa.arrivillaga It's called pass by assignment, but the behavior is what's classically described as pass by value - see stackoverflow.com/questions/29776736/… . 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? Commented Nov 26, 2022 at 13:33

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Remember to add a return subtotal to appetizers!
@Samwise I believe menu option 4 returns subtotal
@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 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)
oh I see -- hard to tell where the loops begin and end with that tiny indentation (:

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.