-3

I don't know why am I doing wrong but it happens this weird error.

This is my code:

import os
from constants import BCOLORS

def clean_screen():
    '''Clean the screen of the console'''
    clear = lambda:os.system('clear')
    clear()


def exit():
    print(BCOLORS.OKBLUE + "See you later, alligator!!!" + BCOLORS.ENDC)


def doReportPreGame():
    print("Informe pre partido")


def doReportPostGame():
    print("Informe post partido")


def doReportMonthly():
    print("Informe mensual")

def mainMenu():
    clean_screen()
    print("SELECCIONA EL TIPO DE INFORME")
    print("=============================")
    print("\t1. Informe Pre-Partido")
    print("\t2. Informe post-partido")
    print("\t3. Informe mensual")
    print("\t0. Salir\n")
    option = None
    while option not in [1, 2, 3, 0]:
        option = int(input("Opción: "))
    switch(option)

def main():
    mainMenu()


def switch(case):
    sw = {
        1: doReportPreGame(),
        2: doReportPostGame(),
        3: doReportMonthly()
    }
    return sw.get(case, exit())


main()

I have created a menu and to select an option I have developed a "switch", so, to decide which option to access I do it with a dictionary. If I entry a 0 I should to receive only this message: See you later, alligator!!!

But, I've got all these others:

/home/josecarlos/Workspace/python/reports/venv/python/bin/python /home/josecarlos/Workspace/python/reports/main.py
SELECCIONA EL TIPO DE INFORME
=============================
        1. Informe Pre-Partido
        2. Informe post-partido
        3. Informe mensual
        0. Salir

Opción: 0
Informe pre partido
Informe post partido
Informe mensual
See you later, alligator!!!

Process finished with exit code 0

If a select another option, I'm still getting the same result and not only the option selected.

/home/josecarlos/Workspace/python/reports/venv/python/bin/python /home/josecarlos/Workspace/python/reports/main.py
SELECCIONA EL TIPO DE INFORME
=============================
        1. Informe Pre-Partido
        2. Informe post-partido
        3. Informe mensual
        0. Salir

Opción: 1
Informe pre partido
Informe post partido
Informe mensual
See you later, alligator!!!

Process finished with exit code 0

What am I doing wrong? Why it happend this?

José Carlos
  • 2,850
  • 12
  • 59
  • 95
  • 2
    It would help if you made a [mre]. A bunch of the code seems irrelevant to the problem. – wjandrea Jun 27 '20 at 18:01
  • 3
    You call all the functions when trying to put them into the `sw` dictionary. Omit the `()` to put the function itself in the dictionary. – mkrieger1 Jun 27 '20 at 18:02
  • 1
    Confirmed, 95% of the code is irrelevant. Here's [my MRE](https://gist.github.com/wjandrea/e011ff79ef0605a7bb0714b3fc6bfc17) – wjandrea Jun 27 '20 at 18:11
  • Does this answer your question? [Using a dictionary as a switch statement in Python](https://stackoverflow.com/questions/21962763/using-a-dictionary-as-a-switch-statement-in-python) – Brian McCutchon Jun 27 '20 at 18:17

1 Answers1

4

When you define the dictionary, you need to save the function as a value not to call it while creating the dictionary. So, your switch() function should look like this:

def switch(case):
    sw = {
        0: exit,   #<-- added this
        1: doReportPreGame,
        2: doReportPostGame,
        3: doReportMonthly
    }
    sw[case]()
Anwarvic
  • 12,156
  • 4
  • 49
  • 69