Dodle Posted December 30, 2008 Report Share Posted December 30, 2008 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 consoleopers = ("+", "-", "*", "/")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 answereval_it(Cmem) Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 30, 2008 Report Share Posted December 30, 2008 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.