Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

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:

[code]
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!")
        sleep(1)
        Menu()
        
    try:                                         # to convert it back to a string
        Eingabe = str(Eingabe)
    except ValueError:
        print("")
        
    xyz = "'"                # Problem part: I want the program to jump to an earlier mentioned Function Func1/Func2
    callfunc = xyz + "Func" + Eingabe + "()" + xyz
    print(callfunc)
    eval(callfunc)

[/code]

1 Answer

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

Hallo!

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:

https://onlinegdb.com/H1jtasYXL

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.
...