Calling Function in Python from combined strings

0 votes
asked Feb 28, 2019 by Chaos02 (120 points)
Ok, so I'm trying to call a function called Func1 by having a menu that lets me input numbers for the options in the menu (

[1] Energy

[2] Weapon systems

etc. Now I want the user to input a number (i.e.1) and the program jumps to that point in the program.
my current code is this:

def Menu():
    width = os.get_terminal_size().columns
    print("Schiffssysteme".center(width, '='))
    print("[1] Energiesystem")
    print("[2] Sonstige Systeme")
    print("=".center(width, '='))
    Eingabe = input("Auswahl: ")
    try:                                                             #to test if input is a number
        Eingabe = int(Eingabe)
    except ValueError:
        print("Bitte Zahl eingeben!")
    try:                                         # to convert it back to a string
        Eingabe = str(Eingabe)
    except ValueError:
    xyz = "'"                # Problem part: I want the program to jump to an earlier mentioned Function Func1/Func2
    callfunc = xyz + "Func" + Eingabe + "()" + xyz


1 Answer

0 votes
answered Feb 19 by Nando Abreu (930 points)


You can create a dictionary with the functions inside there values and the keys would be choices you expect. I developed a solution, please check:

Bis bald!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.