产品需求 明确部分
支持4位以内正整数加减乘除
一次计算只支持一个运算符号
错误场景一律定义 错误输入
UED出图
开发设计变更
输入逻辑判断
接口定义变更 one=1&cal=+&two=1
页面charset设置utf8
前端代码变更
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
</head>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"><body><form id="form-cal" method="get" action="http://localhost:8080/cal"> 计算输入: <input type="text" name="one" value="" id="one"> <input type="text" name="cal" value="" id="cal"> <input type="text" name="two" value="" id="two"> <span class="btn btn-default btn-sm" id="btn-jq-form">计算</span></form><script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><script src="https://cdn.bootcss.com/jquery.form/4.2.2/jquery.form.min.js"></script><script> // jquery.form 方式 $('#btn-jq-form').click(function () { $('#form-cal').ajaxSubmit({ dataType: 'text', success: function (resp) { console.info('返回的数据', resp); $('#resp').html(resp); } }); });</script><p id="resp">结果</p></body></html>
服务端代码变更
# -*- coding: utf-8 -*-
import web
urls = ( '/', 'index', '/cal','cal',)
class index:
def GET(self):
return web.seeother('/static/sample.html')
class cal:
def GET(self):
try:
cal = web.input()['cal'].strip()
one = web.input()['one'].strip()
two = web.input()['two'].strip()
except Exception as e:
print e
return u"错误输入"
#check cal
if cal not in ['+','-','*','/']:
return u"错误输入"
if not one.isalnum() or \
len(one) > 8 or \
not two.isalnum() or \
len(two) > 8 :
return u"错误输入"
if cal == '/' and two == '0':
return u"错误输入"
run = one + cal + two
return "%s %s %s = %s" % (one,cal,two,eval(run))
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
执行一轮冒烟测试,没有发现问题
1+2
1 0 1
网友评论