Issue
i want to make a calculator to "/","*",and "+" so i write my code like that
x,op,y=raw_input()
if op=='+':
print int(x)+int(y)
here if i enter number have two digit it will make error i should enter digit less than 10 form 0 to 9 only to make plus or minus and so on so i tried to split them like this
x,op,y=raw_input().split()
if op=='+':
print int(x)+int(y)
put the input will be like 20 + 20 here is the problem i want to remove this space between the number more than 9 to make the operation i want the input like 20+20 not 20 + 20 on them so i can submit the code on the online judge help me please
Solution
Do you actually need to parse the expression yourself? What about
expression = raw_input()
answer = eval(expression)
print answer
?
You could use try:
and catch exceptions and do something sensible if the default exception raising isn't the behavior you want. (Like, for instance, if the expression ends up being asdf'.8
or some other garbage expression, you might want a different behavior from the default SyntaxError
.)
Note: a criticism of the approach I suggest above is it allows potentially malicious strings to be evaluated, so it might make sense to sanitize your input first...
Answered By - dslack
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.