I'm trying to build a simple calculator in python using tkinter. After - Python
6/7/2021, 10:44:32 AM
I'm trying to build a simple calculator in python using tkinter. After

getting the input stringwith mathematical signs and numbers, is there any function to compute it?

Ответы (33)

Inuk Syooperstar
6/7/2021, 2:28:17 PM

oh god

George K
6/7/2021, 2:34:01 PM

"The difference between stupidity and genius is that genius has it's limits." -Albert Einstein

6/7/2021, 2:35:59 PM

I honestly wasn't expecting anything. Python really amazes me!!

George K
6/7/2021, 2:36:26 PM

Yeah, bad advice at it's finest ❤️

6/7/2021, 2:36:51 PM

what do you mean? I shouldn't use it?

George K
6/7/2021, 2:36:57 PM

Never, except for testing purposes

mosTafa Ar
6/7/2021, 2:37:12 PM

A similar function is found in other languages. But this has a security problem

Jörmungan, Dr.
6/7/2021, 2:37:20 PM

True to his nickname

6/7/2021, 2:37:20 PM

but why? it is working fine

George K
6/7/2021, 2:37:35 PM

Yeah, before exploding in your face.

6/7/2021, 2:38:14 PM

Are you saying it's not correct?

6/7/2021, 2:38:15 PM

can you please elaborate? or point me to a source please

George K
6/7/2021, 2:38:29 PM

Im saying you really ignorant sar.

Артём Юров🇷🇺
6/7/2021, 2:38:57 PM

there is one flaw you should be aware of. It executes any executable expression that was passed there. For example if you host the script at your PC, someone can input a code to format your C drive or sth like that. source google: python eval() docs

George K
6/7/2021, 2:38:58 PM

Eval is like a "try jumping" signal before a pit Sometimes the pit is small, but others there is no floor. And yes, it's a dark souls reference

mosTafa Ar
6/7/2021, 2:39:22 PM

It executes the inputs. So malicious code can be injected

6/7/2021, 2:41:03 PM

I got it now. Thank you guys. So is there any alternative for this?

George K
6/7/2021, 2:41:15 PM

Literal_eval from ast built-in library Or handling the input() correctly with some casting and exception handling.

6/7/2021, 2:42:49 PM

btw a = set(s := (input())) b = {"0","1,...,"9","+","-","*","/"} if len(a.difference(b)) == 0: eval(s) is this safe

Артём Юров🇷🇺
6/7/2021, 2:43:15 PM


George K
6/7/2021, 2:43:36 PM

Eval is never safe and it should never be used in code that is not for testing

mosTafa Ar
6/7/2021, 2:44:11 PM

First, delete the allowed operations from the string. Then you just have to have a number. If it is a number, pass it to the function 2 + 2 Delete spaces and + 22 .isnumeric() ? Yes So Eval("2 + 2")

6/7/2021, 2:45:08 PM

yes someone might still find a flaw that can be used as an exploit, just asking :)

6/7/2021, 2:47:08 PM

again, try literal_eval

6/7/2021, 2:47:43 PM

someone asked for the question, i couldn't tag their name

6/7/2021, 2:49:08 PM

But why no one gave an answer before now? I was only trying to help. Smh you could have given your critism without having to type an Einstein's quote to insult me.

George K
6/7/2021, 2:54:51 PM

well, you shouldnt give bad advice

6/7/2021, 2:55:58 PM

For example, eval for me it's incredibly dangerous to use, I hope it's not the same with global variables

6/7/2021, 2:56:26 PM


6/7/2021, 2:57:28 PM

their values can be changed by anything, it really depends on the code after all.

6/7/2021, 2:58:22 PM


6/7/2021, 2:58:23 PM

Do not use eval. It's a security risk, just like exec. If applicable, consider safe alternatives such as class constructors like int(x) or float(x) in a try-except clause, or ast.literal_eval, which can evaluate Python literals: >>> ast.literal_eval( "[1, 2, 3]" ) [1, 2, 3]

6/7/2021, 2:58:24 PM


Похожие вопросы по теме Python

8/19/2021, 2:15:37 PM
Ответов: 33
How can I solve ax^2+bx+c<0 in python?
2/3/2021, 8:06:49 PM
Ответов: 30
I have an small storage about 64byte and want to save this

code in it can somebody compact it more? nums = [ input for num in range11 ] printnums[10] in nums[:10]

5/3/2021, 5:09:28 PM
Ответов: 27
I want to know what we can do with python?
1/22/2021, 7:32:55 AM
Ответов: 29
How can I update for my python package?
10/1/2020, 7:17:56 PM
Ответов: 29
Like what? In the code editor? I dont understand your question
4/14/2021, 6:35:53 PM
Ответов: 27
Hi, I'm trying to use Pyqt5 to make a GUI. I want

to push a button on it with Right Click of the mouse. I've seen something on Google but I didn't find. How can I do that?

3/22/2021, 11:08:24 AM
Ответов: 29
Hello, I got this code: for i in range 1000000000, 1299999999:

x = 0 printx,i and I would like to make it print without a space between the x and the i .. what should I do?

4/8/2021, 12:07:28 PM
Ответов: 27
How to extract "25APR19" from "ACC25APR191480PE"?
12/18/2020, 8:49:22 PM
Ответов: 26
Guys, is it possible to output an array without commas, brackets and


1/21/2021, 9:23:21 PM
Ответов: 30
Hi, I'm learning the basics of web scraping and i've done a

script that helps me with be up to date with my school's important information, i would like to receive some feedback so can i share with you the github repository's link?