これは私のコードです:私はPythonの予測計算のWebアプリを作るしようとしているフラスコPythonの電卓500内部サーバーエラー
#!/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
。私はエラーを見つけることができません。私は単純な "eval"関数でこれを作ろうとしましたが、うまくいきました。しかし、受け取った入力を計算するために自分の関数を作成すると、エラーが発生します。これをどうやって解決するのですか?
トレースバック:
2017-03-22 06:20:59,184 :[2017-03-22 06:20:59,182] ERROR in app: Exception on/[POST]
2017-03-22 06:20:59,186 :Traceback (most recent call last):
2017-03-22 06:20:59,186 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
2017-03-22 06:20:59,186 : response = self.full_dispatch_request()
2017-03-22 06:20:59,187 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
2017-03-22 06:20:59,187 : rv = self.handle_user_exception(e)
2017-03-22 06:20:59,187 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
2017-03-22 06:20:59,187 : reraise(exc_type, exc_value, tb)
2017-03-22 06:20:59,188 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
2017-03-22 06:20:59,188 : rv = self.dispatch_request()
2017-03-22 06:20:59,188 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
2017-03-22 06:20:59,188 : return self.view_functions[rule.endpoint](**req.view_args)
2017-03-22 06:20:59,188 : File "/home/Firiyuu77/mysite/flask_app.py", line 64, in index
2017-03-22 06:20:59,188 : result = forecast(rem,sold1,sold2)
2017-03-22 06:20:59,188 : File "/home/Firiyuu77/mysite/flask_app.py", line 26, in forecast
2017-03-22 06:20:59,189 : if((rem/3)>=(sold1+10) or (rem/3)>=(sold1+10)):
2017-03-22 06:20:59,189 :TypeError: unsupported operand type(s) for /: 'unicode' and 'int'
ポストそれは感謝@AvinashRajトレースバック私はちょうど更新した –
ことによってそれを変換することができます! – Rekt
'rem'、' sold1'をintに変換します。 –