这是我的代码:
lock.trylock()
我正在尝试制作一个python预测计算器网络应用程序。我找不到错误。我尝试使用简单的“eval”函数进行此操作并且它可以工作,但是当我自己创建函数来计算接收到的输入时,它会得到错误。我该如何解决这个问题?
回溯:
#!/usr/bin/env python
from flask import Flask, request
# create app
app = Flask(__name__)
def calculate(r,t,l):
return ((l+t)/2)*3
def forecast(rem, sold1, sold2):
if (rem == 0 and sold1 == 0 and sold2 ==0): #All ZERO
result = 15
elif (rem == 0 and sold1 == 0 and sold2 < 10): #ALL FOR ONE PRODUCT VALUE
result = sold2*3
elif (rem == 0 and sold1 < 10 and sold2 ==0):
result = sold1*3
elif (rem < 10 and sold1 == 0 and sold2 == 0):
result = rem*3
#END FOR ONE PRODUCT VALUE
elif (rem>= 10 and sold1>=10 and sold2>=10):
if((rem/3)>=(sold1+10) or (rem/3)>=(sold1+10)):
result = 0
else:
result = calculate(rem,sold1,sold2)-rem
elif (rem<10 and sold1<10 and sold2<10):
result = calculate(rem,sold1,sold2)
elif (rem == 0 and sold1>=10 and sold2>=10):
result = calculate(rem,sold1,sold2)
else:
result = sold1
print ("\n\nSuggested stock purchase: ")
return result
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
# show html form
return '''
<form method="post">
<input type="text" name="remaining" />
<input type="text" name="sold1" />
<input type="text" name="sold2" />
<input type="submit" value="forecast" />
</form>
'''
elif request.method == 'POST':
# calculate result
rem = request.form.get('remaining')
sold1 = request.form.get('sold1')
sold2 = request.form.get('sold2')
result = forecast(rem,sold1,sold2)
return 'result: %s' % result
答案 0 :(得分:0)
问题是,烧瓶为str
var提供了rem
。你需要做的就是替换这一行:
rem = request.form.get('remaining')
用这个:
rem = int(request.form.get('remaining'))
您也可以为其他变量执行此操作
答案 1 :(得分:0)
您从表单获得的数据类型不是int
而是unicode
,您可以将它们转换为所需类型int
,然后将其传递给forecast
:
result = forecast(int(rem),int(sold1),int(sold2))
答案 2 :(得分:0)
rem = request.form.get('remaining')
是一个字符串,而不是int。
您可以通过
进行转换 rem = int(request.form.get('remaining'))