运行 模板文件中的函数 (Python/Bottle)
Running function within template file (Python/Bottle)
我正在使用 bottle 创建一个简单的退休计算器,但我在处理 python 代码的模板文件时遇到了问题。例如,我有这个代码
<%
import statistics, numpy
medianStockReturn = []
def stockReturn():
global medianStockReturn
yearStockReturn = numpy.random.normal(11.4, 19.7, 1000)
yearMedianStockReturn = statistics.median(yearStockReturn)
yearMedianStockReturn = yearMedianStockReturn / 100 + 1
medianStockReturn.append(yearMedianStockReturn)
stockReturn()
end
%>
<!DOCTYPE html>
<html lang = "en-us">
<head>
<title>Retirement Calculator</title>
<meta charset = "utf-8">
<link rel="stylesheet" type="text/css" href="../static/retirementStyle.css">
</head>
<body>
<h2> this is a test; your stock return is {{medianStockReturn}}</h2>
</body>
</html>
但是,此代码的输出结果为:"this is a test; your stock return is []"
正如所写,该函数应该将生成的值附加到 medianStockReturn 上,但它没有,我也不确定为什么。
您需要将 end
移动到上面函数定义结束后 stockReturn()
<%
import statistics, numpy
medianStockReturn = []
def stockReturn():
global medianStockReturn
yearStockReturn = numpy.random.normal(11.4, 19.7, 1000)
yearMedianStockReturn = statistics.median(yearStockReturn)
yearMedianStockReturn = yearMedianStockReturn / 100 + 1
medianStockReturn.append(yearMedianStockReturn)
end
stockReturn()
%>
<!DOCTYPE html>
<html lang = "en-us">
<head>
<title>Retirement Calculator</title>
<meta charset = "utf-8">
<link rel="stylesheet" type="text/css" href="../static/retirementStyle.css">
</head>
<body>
<h2> this is a test; your stock return is {{medianStockReturn}}</h2>
</body>
</html>
我正在使用 bottle 创建一个简单的退休计算器,但我在处理 python 代码的模板文件时遇到了问题。例如,我有这个代码
<%
import statistics, numpy
medianStockReturn = []
def stockReturn():
global medianStockReturn
yearStockReturn = numpy.random.normal(11.4, 19.7, 1000)
yearMedianStockReturn = statistics.median(yearStockReturn)
yearMedianStockReturn = yearMedianStockReturn / 100 + 1
medianStockReturn.append(yearMedianStockReturn)
stockReturn()
end
%>
<!DOCTYPE html>
<html lang = "en-us">
<head>
<title>Retirement Calculator</title>
<meta charset = "utf-8">
<link rel="stylesheet" type="text/css" href="../static/retirementStyle.css">
</head>
<body>
<h2> this is a test; your stock return is {{medianStockReturn}}</h2>
</body>
</html>
但是,此代码的输出结果为:"this is a test; your stock return is []"
正如所写,该函数应该将生成的值附加到 medianStockReturn 上,但它没有,我也不确定为什么。
您需要将 end
移动到上面函数定义结束后 stockReturn()
<%
import statistics, numpy
medianStockReturn = []
def stockReturn():
global medianStockReturn
yearStockReturn = numpy.random.normal(11.4, 19.7, 1000)
yearMedianStockReturn = statistics.median(yearStockReturn)
yearMedianStockReturn = yearMedianStockReturn / 100 + 1
medianStockReturn.append(yearMedianStockReturn)
end
stockReturn()
%>
<!DOCTYPE html>
<html lang = "en-us">
<head>
<title>Retirement Calculator</title>
<meta charset = "utf-8">
<link rel="stylesheet" type="text/css" href="../static/retirementStyle.css">
</head>
<body>
<h2> this is a test; your stock return is {{medianStockReturn}}</h2>
</body>
</html>