Jump to content

Operators within strings


Recommended Posts

I have a list:

Cmem = ["10", "-", "5"]

How can I extract Cmem[1] and change it from a string to a literal operator?

I know that with numbers I can use either int(Cmem[1]) or float(Cmem[1]).

Full Code:

# wxCalc v0.0.0.3 console

opers = ("+", "-", "*", "/")
Cmem = ["10", "-", "5"]

def eval_it(Cmem):
for O in opers:
if O in Cmem:
m1 = Cmem.index(O)-1
a1 = Cmem.index(O)+1
val1 = Cmem[m1]
val2 = Cmem[a1]
answer = val1 O val2
print answer

eval_it(Cmem)

Link to comment
Share on other sites

I don't think that any compiler can understand that. I think this is something that would need to be done in a precompiler run. As long as your strings are constants only, it will work; it will not work if the values are supplied at run time.

Or, you would need to create a function that does things according to the operator value, e.g. with some sort of Select / Case statement (I don't know if python has such an operation).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy