码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【python&flask-1】简单实现加减乘除输入界面


    app.py

    1. import flask
    2. from flask import Flask, render_template, request
    3. # 计算精确的浮点结果,float加法也计算不出来
    4. from decimal import Decimal
    5. app = Flask(__name__)
    6. @app.route('/')
    7. def home():
    8. return render_template('index.html')
    9. @app.route('/calculate', methods=['POST'])
    10. # POST请求处理用户提交的数据
    11. def calculate():
    12. num1 = Decimal(request.form['num1'])
    13. num2 = Decimal(request.form['num2'])
    14. operation = request.form['operation']
    15. result = Decimal(0)
    16. # 输入的两个数和运算符,结果初始为0
    17. if operation == 'add':
    18. result = num1 + num2
    19. # 加
    20. elif operation == 'subtract':
    21. result = num1 - num2
    22. # 减
    23. elif operation == 'multiply':
    24. result = num1 * num2
    25. # 乘
    26. elif operation == 'divide':
    27. if num2 != Decimal(0):
    28. result = num1 / num2
    29. else:
    30. return "错误:分母不能为0"
    31. # 除
    32. result = round(result,4)
    33. # 保留四位小数
    34. return render_template('result.html', num1=num1, num2=num2, operation=operation, result=result)
    35. # 然后将计算结果和输入的两个参数返回给result.html渲染
    36. if __name__ == '__main__':
    37. app.run(debug=True)

    templates文件夹

    index.html

    1. html>
    2. <html>
    3. <head>
    4. <title>在线计算器title>
    5. head>
    6. <body>
    7. <h1>在线计算器h1>
    8. <form action="/calculate" method="POST">
    9. <input type="text" name="num1" required>
    10. <select name="operation" required>
    11. <option value="add">+option>
    12. <option value="subtract">-option>
    13. <option value="multiply">*option>
    14. <option value="divide">/option>
    15. select>
    16. <input type="text" name="num2" required>
    17. <button type="submit">开始计算button>
    18. form>
    19. body>
    20. html>

    result.html

    1. html>
    2. <html>
    3. <head>
    4. <title>计算结果title>
    5. head>
    6. <body>
    7. <h1>计算结果h1>
    8. <p>{{ num1 }} {{ operation }} {{ num2 }} = {{ result }}p>
    9. body>
    10. html>

    实现效果

    支持小数点计算

  • 相关阅读:
    JUC源码学习笔记1——AQS和ReentrantLock
    算法通关村第六村-白银挑战树的层序遍历
    Google 开源库Guava详解(集合工具类)—Maps、Multisets、Multimaps
    Dubbo client can not supported string message
    日期时间格式化 @JsonFormat与@DateTimeFormat
    软件测试概念
    错误:找不到或无法加载主类
    Springcloud笔记(1)-微服务和springcloud介绍
    遗传算法bp神经网络原理,bp神经网络 遗传算法
    全量知识系统问题及SmartChat给出的答复 之18 生存拓扑控制+因子分析实现自然语言处理中的特征提取及语义关联
  • 原文地址:https://blog.csdn.net/weixin_56463218/article/details/132852074
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号