简体   繁体   English

使用Brython代码进行多次计算

[英]Using Brython code with more than one calculation

I'm trying to start using Brython in an on-line course. 我正在尝试在在线课程中使用Brython。 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. 为了测试它,我创建了一个简单的单位转换练习,学生以mph为单位填写速度,以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) 下面是此简单练习的代码(我尚未加载MathJax,因此$符号)

...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>

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. 甚至在复制和粘贴的代码注释上也起作用:第二个脚本检索回第一个表单,并尝试在那里更改不存在的“ vmph2”控件。 The second function should take the index [1] from the document forms. 第二个函数应从文档表格中获取索引[1] 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"] . 但是,除了获取表格(您的fh和fh2变量)然后进行控制之外,您还可以简单地使用document["vmph"]document["vmph2"]获取对相关标签的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM