title: How to let user use "sqrt()" + input to find square root of input?
body: <p>I am still learning python so please be nice :) I want to let the user to do something like this:</p>
  
  <pre><code>&gt;&gt; sqrt(4) #input
  2 #output
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&gt;&gt; sqrt(8) #input
  2*rad(2) #if this is not possible, just say something like "not valid"
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&gt;&gt; 4*4 #1st input
  16 #1st output
  &gt;&gt; sqrt(ans()) #2nd input
  4 #2nd output
  </code></pre>
  
  <p>My code: </p>
  
  <pre><code>from math import sqrt
  valid_chars = "0123456789-+/*ansqrt() \n"
  while True:
      x = "x="
      y = input(" &gt;&gt; ")
      x += y
      def ans():
          try:
              return z
          except NameError:
              return 0
      if(y == "end" or y == "End" or y == "exit" or y == "Exit" or y == "cancel" or y == "Cancel"):
                          break
      if any(c not in valid_chars for c in y):
          print("WARNING: Invalid Equation")
          continue
      try:
          exec(x)
      except (SyntaxError, ZeroDivisionError, NameError, TypeError, ValueError):
          print ("WARNING: Invalid Equation")
      if not(y == "()"):
          z = x
          print(x)
      else:
          print("WARNING: Invalid Equation")
  </code></pre>
  
  <p>And if the user inputs something like this:</p>
  
  <pre><code>&gt;&gt; sqrt()x
  WARNING: Invalid Equation
  </code></pre>
  
  <p>I know that I have to change the valid_chars to this:</p>
  
  <pre><code>valid_chars = "0123456789-+/*ansqrt() \n"
  </code></pre>
  
  <p>or something. However, for the rest, I have almost no clue. Thank you in advance, if you have any questions or think that this is incomplete, please comment about it! :) Also, don't for get to answer :D</p>
  
  <p>Update:</p>
  
  <p>Some ideas:</p>
  
  <p>Take the value inside or outside of the "()". Then make that value equal to for example "s". After that: </p>
  
  <pre><code>d = sqrt((int(s)))
  print (d)
  </code></pre>
  
  <p>However, I need something that will exclude inputs that causes errors so the program does not crash.</p>
  
  <p>Also, I am not making this public so the you can ignore the fact that taking python statement as input is a bad idea. However, I am interested in other ways on how to do the same thing as eval(). Thanks</p>
  
  <p>Updated code:</p>
  
  <p>Problem fixed:</p>
  
  <pre><code>                from math import sort
                  valid_chars = "0123456789-+/*ansqrt() \n";
                  while True:
                      x = "x="
                      y = input(" &gt;&gt; ")
                      x += y
                      def ans():
                          try:
                              return z
                          except NameError:
                              return 0
                      if(y == "end" or y == "End" or y == "exit" or y == "Exit" or y == "cancel" or y == "Cancel"):
                          break
                      if any(c not in valid_chars for c in y):
                          print("WARNING: Invalid Equation")
                          continue
                      try:
                          exec(x)
                      except (SyntaxError, ZeroDivisionError, NameError, TypeError, ValueError):
                          print ("WARNING: Invalid Equation")
                      else:
                          if not(y == "()"):
                              z = x
                              print(x)
                          else:
                              print("WARNING: Invalid Equation")
  </code></pre>