0

I'm trying to create a virtual restaurant menu, and clicking a button assigns a number that corresponds to the index of an appetizer and entree list I created earlier. I'm not sure how to keep the value of app_ix and entree_ix because I can't call the meal() function without those values and pressing escape will erase those values and cause a local variable error where the entree_ix and app_ix don't have a value, yet they are being called as arguments for the meal() function.

def screen(app, ent):
    mx, my = pygame.mouse.get_pos()
    if app == 1:
        running = True
        while running:
            window.fill(WHITE)
            window.blit(menu, (0, 0))
            app_box_one = pygame.Rect(WIDTH/3 - 100, HEIGHT/5, 400, 100)
            pygame.draw.rect(window, NUTMEG, app_box_one)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if app_box_one.collidepoint(mx, my):
                        running = False
                        app_ix = 0
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running = False

            pygame.display.update()
            main_clock.tick(60)
    if ent == 1:
        running = True
        while running:
            window.fill(WHITE)
            window.blit(menu, (0, 0))
            option_box_one = pygame.Rect(WIDTH/3 - 100, HEIGHT/5, 400, 100)
            pygame.draw.rect(window, NUTMEG, option_box_one)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if option_box_one.collidepoint(mx, my):
                        running = False
                        entree_ix = 0
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running = False

            pygame.display.update()
            main_clock.tick(60)

def meal(app_index, entree_index):
    window.fill(WHITE)
    window.blit(menu, (0, 0))
    meal_font = pygame.font.SysFont('corbel', 50, True)
    meal_message = app_list[app_index] + entree_list[entree_index]
    final_meal_message = meal_font.render(meal_message, True, BLACK)
    window.blit(final_meal_message, (WIDTH/2 - 50, HEIGHT/2 - 50))
    pygame.display.update



main()
Anonymous
  • 709
  • 4
  • 15
  • 34
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. – Prune Feb 17 '21 at 22:54
  • You've posted 50 lines of code for what appears to be a question of perhaps 8 lines -- and is already answered in any tutorial on functions. Look up the scoping rules: those are *not* the same variable. Repeat your materials on functions; learn how to return a value to the calling program. You *could* do it with a `global` variable, but that's poor programming practice. – Prune Feb 17 '21 at 22:55
  • im not entirly sure, but i just learnt about global variables from ```sentdex's``` video and he mentioned an Unbound local error so you may want to check out global variables (i may be totally wrong, i am a beginner) but good luck on finding a solution! – some_user_3 Feb 17 '21 at 22:56
  • @Prune what should i do instead of a ```global variable``` if it is bad programming practice? – some_user_3 Feb 17 '21 at 22:57

1 Answers1

0

You should declare a global variable inside of the function.

def test():
    global gVar
    gVar = 10
    # Do stuff #

test()
print(gVar)

How ever this is bad programming practice, generally if you need to do this, you are doing something else wrong.

carloslockward
  • 185
  • 2
  • 6