title: Python-How to convert function argument into numeric operator/float()/int()?
body: <p>What I tried to do:</p>
  
  <blockquote>
    <p>Create a calculator application. Write code that will take two numbers
    and an operator in the format: N1 OP N2, where N1 and N2 are floating point or
    integer values, and OP is one of the following: +, -, <em>, /, %, *</em>, representing
    addition, subtraction, multiplication, division, modulus/remainder, and
    exponentiation, respectively, and displays the result of carrying out that operation
    on the input operands.</p>
  </blockquote>
  
  <p>What I was able to come up with:</p>
  
  <pre><code>def calculator(n1,op,n2):
      n1 = float(n1)
      n2 = float(n2)
      if op == "+":
          return (n1 + n2)
      elif op == "-":
          return (n1 - n2)
      elif op == "*":
          return (n1 * n2)
      elif op == "/":
          return (n1 / n2)
      elif op == "%":
          return (n1 % n2)
      elif op == "**":
          return (n1 ** n2)
  </code></pre>
  
  <p>It works. But there might be 2 potential improvements: </p>
  
  <ol>
  <li><p>right now one has to use double quotes(<code>""</code>) when entering the operator, for example, calculator(3,"+",3). Otherwise the interpreter returns a <code>SyntaxError</code> pop-up. I tried to change <code>if op == "+":</code> into <code>if op == +:</code>, but then the interpreter returns a SyntaxError, highlighting the <code>:</code> after the <code>+</code>. </p></li>
  <li><p>right now the function converts all kinds of number input into <code>float()</code>, even if integer was taken as input. How to let the function itself determine whether the input is integer or floating point, and convert it accordingly? </p></li>
  </ol>
  
  <p>I read the Documentation on Functions, but it only talked about several types of arguments, and none of them seem to help solving the current problems. I'm sure this is pretty basic stuff, but as a beginner I tried and wasn't able to figure it out.</p>