1

Let's say I have a string named product.

product may be string such as "5*2" or "12/3".

Does anyone know a way I can compute the output of a product or dividend such as this? Considering sum is a string.

Kashmiryy
  • 13
  • 3
  • FYI you shouldn't name something `sum` in Python because it shadows the built-in function with the same name. Same goes for any built-in names, of course. – Random Davis Jun 14 '22 at 20:19
  • @RandomDavis Changed sum to product, thanks. – Kashmiryy Jun 14 '22 at 20:20
  • "Does anyone know a way I can compute the output of a product or dividend such as this? " Well, what do you imagine are the logical steps involved in solving the problem? Where exactly are you stuck? For example, can you write code to determine the multiplicands (or dividend and divisor)? Can you write code to perform a multiplication (or division)? If you put these two things together, why does it not solve the problem? Please read [ask] and https://meta.stackoverflow.com/questions/261592. – Karl Knechtel Jun 14 '22 at 20:30
  • For anyone in the future, I fixed it by simply consulting https://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string – Kashmiryy Jun 15 '22 at 15:43

1 Answers1

1

check whether "/" or "*" are in your string and split it accordingly :)

product  = "12/3"
if "*" in product:
    first,sec = product.split("*")
    print(float(first)*float(sec))
elif "/" in product:
    first,sec = product.split("/")
    print(float(first)/float(sec))
Ohad Sharet
  • 1,097
  • 9
  • 15