How to use words instead of numbers to do addition/subtraction in python?

New to python here. I am working on a problem to write a script that gets user input in the form of a word problem such as “two plus three” and “seven minus five”, but returns a numerical value as the output. For example, if the user enters "two plus three" the output should be 5 (assuming the user only enters numbers 0-9 and operations plus, minus, times, and divided by).

I'm thinking I need to break apart the string into the numbers and the operation. Should I use .split? And how do I get the spelled-out number to process as a numerical value?

1 Answer

  • EddieJ
    Lv 7
    2 months ago

    You probably should use .split ().  

    You could use a dictionary to convert the words into numbers, if you have learned about those.

    You would probably use if / elif to process the operator.

    Try to write some code and if you have trouble, show us what you have.

    • Login to reply the answers
Still have questions? Get your answers by asking now.