-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2_forms_calc.py
64 lines (53 loc) · 1.81 KB
/
part2_forms_calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/sum')
def sum_numbers():
x1 = request.args.get('x')
y1 = request.args.get('y')
return '<h2> {} + {} = {} </h2>'.format(x1, y1, int(x1) + int(y1))
@app.route('/calculator', methods=['GET', 'POST'])
def calculator():
l = 'this is my calculator'.split()
result = None # when the method is GET
if request.method == 'POST':
if not request.form['num1'] and not request.form['num2'] \
and not request.form['op']:
result = 'please fil the form'
# print(result)
else:
try:
n1 = request.form['num1']
n2 = request.form['num2']
op = request.form['op']
# print(n1, op, n2)
result = eval('{}{}{}'.format(n1, op, n2))
# print(result)
except BaseException as error:
result = 'error: {}'.format(error)
# print(result)
return render_template('calculator.html', result=result, l=l)
@app.route('/calculator2', methods=['GET', 'POST'])
def calculator2():
result = ''
n1 = ''
n2 = ''
op = ''
if request.method == 'POST':
n1 = request.form['num1']
n2 = request.form['num2']
op = request.form['op']
if n1 and n2:
try:
result = eval('{}{}{}'.format(n1, op, n2))
print(result)
except BaseException as error:
result = 'error: {}'.format(error)
print(result)
else:
result = 'please fill the form'
return render_template('calculator2.html', n1=n1, op=op, n2=n2, result=result)
if __name__ == '__main__':
app.run(debug=True)