[Python] Safely evaluate simple string equation

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I'm writing a program in which an equation is inputted as a string, then evaluated. So far, I've come up with this:

    test_24_string = str(input("Enter your answer: "))
    test_24 = eval(test_24_string)


    I need both a string version of this equation and an evaluated version. However, eval is a very dangerous function. Using int() doesn't work, though, because it's an equation. Is there a Python function that will evaluate a mathematical expression from a string, as if inputting a number?

    Continue reading...

Compartilhe esta Página