-2

I have a calculator which makes calculations via the command eval. Eval is a built in command in python to calculate math equations, but the equation does not work if you type 05*5 how can i make it so that when i type 05 it take the entry and deletes the 0 out of the list and then sends it to eval and make it 5*5 instead of 05?

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • 2
    Provide your code – Sahadev May 20 '18 at 06:19
  • 3
    Don't use eval: https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – awesoon May 20 '18 at 06:25
  • `05` isn't a valid integer literal in Python 3. In Python 2, a leading zero indicates `octal`, so `010 == 8`. This bit too many people, so you now need to do eg `0o10` for octal numerals. – PM 2Ring May 20 '18 at 06:26
  • And look at this about why numbers with leading zeroes does not work: https://stackoverflow.com/questions/13013638/python-cannot-handle-numbers-string-starting-with-0-why – awesoon May 20 '18 at 06:27
  • Take a look at the `str.lstrip` method. But really, it would be better to avoid using `eval`, it's just too dangerous. – PM 2Ring May 20 '18 at 06:28
  • im already deep into my calculator so what should i use to make it calculate the asnwer setart or str.lstrip – John Smith May 20 '18 at 06:31
  • 3
    "Eval is a built in command in python to calculate math equations" that is not what eval is for. It is for dynamically *evaluating* strings that are valid Python expressions. The task you are trying to tackle, parsing input that represents arithmetic expressions, is not as trivial as it may first appear. You use the solution of relying on the Python parser to process your string. If you don't want it to work like Python, then preprocess the string, or write your own parser. Either would be a good learning endeavor – juanpa.arrivillaga May 20 '18 at 06:33
  • Making a fancy calculator without using `eval` takes a bit of work. So the simple solution is to keep using `eval`, and if someone enters stuff that's not valid Python, your calculator should treat it as a Syntax Error, just like Python does. You can use Python's `try`... `except SyntaxError` to trap such errors. – PM 2Ring May 20 '18 at 07:04
  • Can anyone asnwer my orignal question about the 05, does anyone know how to make the variable of what the user has entered and delete all 0s not followed by a number or decimal point and eval that ? – John Smith May 20 '18 at 07:17
  • For the example in the question it's easy (just use `.lstrip`) but in general it's tricky, because stuff like '05' could be buried deep in an expression, like `3+4*(11-05)`. One way to handle that would be to use [Regular Expressions](https://docs.python.org/3/library/re.html), aka regex. And of course, you'd need to deal with situations where the user _wants_ leading zeros, like `0x2c*4`. – PM 2Ring May 20 '18 at 07:42

1 Answers1

0

When you store 05 in a variable then it automatically converted to 5 but if you are taking 05 as an input from the user then you should first cast it into int type and it will automatically be converted into 5.

If that doesn't solve your problem then please provide the code so I can find out what's the problem.

Naveen
  • 56
  • 3