0

I'm trying to start using Brython in an on-line course. In order to test it I created a simple unit-conversion exercise where a student fills in a speed in mph and gets back the speed in ft/s. Works fine. But I find that any second calculation I add is ignored. Something needs to be zeroed out, or flushed, or reset or something! Any advice would be appreciated. The code for this simple exercise is below (I've not loaded MathJax, so the $ signs)

...you'll see that the first one works fine and the second one, while identical except for changing the names of the function and all variables is simply ignored.

Thanks!

<HTML>
<HEAD>
<META charset="utf-8">
<script type="text/javascript"
    src="https://cdn.rawgit.com/brython-dev/brython/3.3.5/www/src/brython.js">
</script>
<script type="text/javascript"
    src="https://cdn.rawgit.com/brython-    dev/brython/3.3.5/www/src/brython_stdlib.js">
</script>
</HEAD>
<BODY bgcolor="white" onload="brython(1)">
<H1>test</H1>
<!-- silly test example anticipating multiple unit conversions in a row: -->
<!-- This will work if I remove the previous script and form...but won't if it follows -->

<SCRIPT type ="text/python"> 
import math 
from browser import document

@document["vmph"].bind("change") 
def gcal(xx): 
    # get the first element with tag "form" in the document
    fh = document.select("form")[0] 
    vvmph = float(fh.vmph.value) 
    vvftps = vvmph*1.4666700004 
    fh.vftps.value = vvftps 
</SCRIPT>


<FORM method="" action=""> 
    <p class="ex1">For $v$ mph = <INPUT Type="text" Name="vmph" id="vmph"     Value="" Size="10" autocomplete="off"> $\;\;$mph<br> 
    we get that $v$ ft per second = <INPUT Type="text" Name="vftps" Value=""     Size="10">. 
    </p> 

</FORM>


<SCRIPT type ="text/python">            
import math 
from browser import document

@document["vmph2"].bind("change") 
def gcal2(xxx): 
    # get the first element with tag "form" in the document
    fh2 = document.select("form")[0] 
    vvmph2 = float(fh2.vmph2.value) 
    vvftps2 = vvmph2*1.4666700004 
    fh2.vftps2.value = vvftps2 
</SCRIPT>

<FORM method="" action=""> 
    <p class="ex1">For $v$ mph = <INPUT Type="text" Name="vmph2" id="vmph2" Value="" Size="10" autocomplete="off"> $\;\;$mph<br> 
    we get that $v$ ft per second = <INPUT Type="text" Name="vftps2" Value="" Size="10">. 
    </p> 

</FORM>
</BODY>
</HTML>

1 Answers1

0

the working is even on the comments on the code you copy and pasted: the second script retrieves back the first form, and try to change the non-existant "vmph2" control there. The second function should take the index [1] from the document forms. But instead of getting to the form (yours fh and fh2 variables), and then proceed to the control, you could simply get a reference to the relevant tag with document["vmph"] and document["vmph2"] .

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • Thank you. So the second one now reads fh2 = document.select("form")[1] and that works. Can you advise on one other matter in this context? In javascript often unit conversions of this sort often are bi-directional. For example, if one typed in "60" for mph in the first field you'd get "88" in the second field...but if you typed in say "53" in the second field, you'd then get 50 back in that first field. I suspect there's a way to make that work here as well...do you know how can I make this also "bi-directional"? I appreciate your help. – Raymond Brock May 23 '18 at 19:49
  • Just teh same you'd do in Javascript - bind the second field to another function, that will make the reverse change. Gven the code above, you could actually refactor things and improve greatly on the total number of lines of code. – jsbueno May 23 '18 at 22:00
  • Thanks for your comments. I'm just trying to solve a very particular problem for some writing and not a modern programmer. Can you be more specific on the binding solution? – Raymond Brock May 27 '18 at 12:51