0

Here's an example:

a = [] #a is an empty list 
a.append(input())  #input is 5*5
print(a[0])
5*5  

How can I print(a[0]) so it works out 5*5 with the result being 25? Is there any way? Thank you

Easter
  • 3
  • 1
  • This is a complex topic, see https://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string – Kyle Berezin Jun 15 '17 at 23:24

1 Answers1

0

The programmer in me hates the following piece of code but if you trust the input, it works well

print(eval(a[0]))

eval tries to treat the string as an expression and evaluate it. In this case, the string '5*5' is treated as simply the python expression 5*5 and outputs

25
Kyle O'Brien
  • 932
  • 1
  • 11
  • 28
  • 2
    Don't **ever** allow users to provide input for the system `eval()`, bad things can and will happen. – zwer Jun 15 '17 at 23:31
  • Thank you so much, You're a life saver. I did vote up your comment but apparently It doesn't show due to being new to the site. – Easter Jun 15 '17 at 23:31
  • Care to explain @zwer? – Easter Jun 15 '17 at 23:32
  • It takes the code inside the string, and literally executes it. Which is why `eval("5*5")` works. It returns the value of the statement`5*5`. Now you can run ANY code you want, by typing things such as `coins = "500000000000000"`, if the eval statement isn't used correctly. –  Jun 15 '17 at 23:33
  • @zwer is 95% correct. This is not good practice. It depends where your input is coming from and the context in which it runs. There are a number of scenarios that this works really well - even on a project I'm currently working on. How you separate this code from the rest of your code is important. That being said, I believe it answers the immediate question around how to execute that expression. – Kyle O'Brien Jun 15 '17 at 23:35
  • @Tobi, I'm getting the result I was after. I'm a newby doing newby little things. This answered my question perfectly. – Easter Jun 15 '17 at 23:35
  • 1
    @Easter - read, for example, [Eval really is dangerous](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) – zwer Jun 15 '17 at 23:35
  • 1
    I'm just saying this is a very lazy solution, that can very easily go wrong. I understand that you're probably contempt with it. –  Jun 15 '17 at 23:36
  • @Easter I'm glad it gave you what you need. Consider the other comments to this answer and if you ever start asking yourself 'is this safe', search for some more material on StackOverflow to perhaps find a new way of doing this. For now, I think you'll be good. Good luck! – Kyle O'Brien Jun 15 '17 at 23:39
  • @Tobi - I believe you have the option of providing an alternative answer :-). I'd think the comment you made was fairly lazy itself.... – Kyle O'Brien Jun 15 '17 at 23:40
  • I appreciate all the answers, Time to sleep as I've spent too long trying to figure this out. One last question: If I think of a little script to make but after hours on hours I can't figure out how to do everything. Is it better to look at someone elses script and see what they did or ask questions on the things i don't know and go from there? – Easter Jun 15 '17 at 23:45
  • @Easter - I'm assuming you mean specifically in the context of Stackoverflow. The steps are: 1. Try it yourself. Mess around, try and get a nitty gritty feel for how it works. You can do this in a python shell (or, better yet, an iPython shell). 2. If you don't come right, Google and see what other people are doing. Search terms are a skill so try and change up what you're searching for. Look at other peoples' implementations and try to replicate them. Break them a part to truly understand. 3. Search Stackoverflow for a similar question 4. After all of that, then open your own question. – Kyle O'Brien Jun 15 '17 at 23:47
  • Thank you @KyleO'Brien. Appreciate your time – Easter Jun 15 '17 at 23:51