News:

HOT TIP! Exploring the commands packaged in VC's standard configurations is a great way to learn how to build your own. You can also clone a command as a starting point: just right-click and select "Clone" (or type Alt-C).

Main Menu

Calculating Square root in Python

Started by Haddood, August 11, 2014, 04:36:06 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Haddood

I am trying to calculate the square root of subtracting 2 numbers ...

I tried
sqrt (x)
I get error global name sqrt not defined

I tried
math.sqrt
I get error global name math not defined

not sure how to do it  :bonk :bonk ... help appreciated

def distance(x,y):
distance= sqrt(x-y)
return str(distance)
When Voice command gets tough, use hand gestures

Kalle

#1
Hi Haddood, I tried following in VC and it works

PY.ExecString   result=({1}{3}{2})**.5

{1} is x
{2} is y
{3} is the computationally sign

here the command group (the math.xml is required in the VC payload folder)

<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.0.5-->
<commandGroup open="True" name="math square root" enabled="True" prefix="" priority="0" requiredProcess="" description="">
 <command id="620" name="square root" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
   <action>
     <cmdType>PY.ExecString</cmdType>
     <params>
       <param>result=({1}{3}{2})**.5</param>
     </params>
     <cmdRepeat>1</cmdRepeat>
   </action>
   <action>
     <cmdType>OSD.ShowText</cmdType>
     <params>
       <param>{LastResult}</param>
     </params>
     <cmdRepeat>1</cmdRepeat>
   </action>
   <phrase>square root</phrase>
   <phrase>from</phrase>
   <payloadRange>1,5000</payloadRange>
   <payloadFromXML phraseOnly="False" use2partPhrase="False" phraseConnector="by" Phrase2wildcard="anyone" optional="False">payloads\math.xml</payloadFromXML>
   <payloadRange>1,5000</payloadRange>
 </command>
</commandGroup>


The python code will not work with negative numbers!
***********  get excited and make things  **********

jitterjames

If you want to use math.sqrt(x)

then at the top of your python code you need to write

import math

jitterjames

also your distance function does not look right.

try this:
import math

def distance(x,y):
   temp = x**2 + y**2
   distance= math.sqrt(temp)
   return str(distance)

print distance(5,6)

Haddood

Many thanx James ... That did the trick ... My learning curve of python is pretty flat  :bonk

Kalle thanks for the feedback, I needed to solve it within python ... I will keep you method for when i need sqrt inside VC
When Voice command gets tough, use hand gestures