+ 1

I want to make a calculator which takes input in one line.

It take input 3 times I want a calculator which takes only 1 input. a = int(input(). b = input() c = int(input()) if b == '+' : print (a + d) elif b == '-' : print (a - d) elif b == '*' : print (a * d) elif b == '/' : print (a / d). else: print ("Error")

28th Nov 2020, 5:06 AM
Sarthak Sharma
Sarthak Sharma - avatar
6 Respuestas
+ 4
Tibor Santa There are definitely circumstances where eval() can cause harm, but here the code will either be run on (1) the computer of the person doing the input, and why would anyone sabotage their own system? Or (2) the SoloLearn server, in which case you don't need eval() to run malicious code, you simply run the code (and hopefully SL will catch it.) The danger is allowing someone else to run malicious code on YOUR computer, and I don't see how this is possible here, but please correct me if I'm wrong.
28th Nov 2020, 7:28 AM
David Ashton
David Ashton - avatar
+ 3
How about print(eval(input())) If you input 2+3/4 You will get 2.75 If you input (3**2+4**2)**.5 You will get 5 🙂
28th Nov 2020, 6:51 AM
David Ashton
David Ashton - avatar
+ 3
David Ashton please don't teach newbies how to plant severe security holes in their programs 🤣😆😅 Seriously, yes eval works, but you should NEVER use it in a real program, especially not in connection with user input!
28th Nov 2020, 7:14 AM
Tibor Santa
Tibor Santa - avatar
+ 3
I think the main harm here is learning a coding pattern, that is normally discouraged and shunned in a professional setting. Sure, for your own hobby project it will not cause a nuclear disaster. But if you take the idea outside and start using it routinely to solve problems, it can quickly escalate into a bad habit.
28th Nov 2020, 7:38 AM
Tibor Santa
Tibor Santa - avatar
+ 2
So you want the user to enter something like this: 42/6 Then you need to "parse" this string, that means to identify the numbers and symbols, so you can determine what operation to perform. The most convenient way would be to use regular expressions, but that could be a little bit rough for a beginner. Anyway the Python course does cover it. https://www.sololearn.com/learn/Python/2475/ If you simplify the problem and make the assumption that the input must be 3 characters, and you only accept single digits, then you can use indexing on the input string, for example a = int(text[0]) op = text[1] Or you can make a rule that there must be a space between numbers and operand, and use the split() function to separate the input data into a list.
28th Nov 2020, 5:26 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Thanks
29th Nov 2020, 3:41 AM
Sarthak Sharma
Sarthak Sharma - avatar