VoxCommando

Help and Support (Using VoxCommando) => Python Scripting => Topic started by: Haddood on August 11, 2014, 04:36:06 AM

Title: Calculating Square root in Python
Post by: Haddood on August 11, 2014, 04:36:06 AM
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

Code: [Select]
def distance(x,y):
distance= sqrt(x-y)
return str(distance)
Title: Re: Calculating Square root in Python
Post by: Kalle on August 11, 2014, 08:33:27 AM
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)

Code: [Select]
<?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!
Title: Re: Calculating Square root in Python
Post by: jitterjames on August 11, 2014, 08:49:19 AM
If you want to use math.sqrt(x)

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

import math
Title: Re: Calculating Square root in Python
Post by: jitterjames on August 11, 2014, 08:56:07 AM
also your distance function does not look right.

try this:
Code: [Select]
import math

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

print distance(5,6)
Title: Re: Calculating Square root in Python
Post by: Haddood on August 13, 2014, 08:19:49 AM
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