python学习吧 关注:15,437贴子:75,531
  • 3回复贴,共1
求助

请求支援!

只看楼主收藏回复

这个代码怎么弄


IP属地:广西来自Android客户端1楼2024-10-22 08:46回复
    from flask import Flask, request, render_template_string
    app = Flask(__name__)
    # 定义 HTML 模板,加入居中样式
    template = '''
    <!doctype html>
    <html lang="zh-CN">
    <head>
    <meta charset="UTF-8">
    <title>Find Numbers</title>
    <style>
    body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    font-family: Arial, sans-serif;
    }
    .container {
    text-align: center;
    width: 300px;
    padding: 20px;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    }
    h2 {
    font-size: 1.5em;
    margin-bottom: 20px;
    }
    input[type="number"], input[type="submit"] {
    padding: 8px;
    font-size: 1em;
    width: 100%;
    margin-top: 10px;
    margin-bottom: 10px;
    box-sizing: border-box;
    }
    </style>
    </head>
    <body>
    <div class="container">
    <h2>请输入一个整数 n:</h2>
    <form method="POST">
    <input type="number" name="n" required>
    <input type="submit" value="提交">
    </form>
    {% if result is not none %}
    <h3>满足条件的数字列表为:</h3>
    <p>{{ result }}</p>
    {% endif %}
    </div>
    </body>
    </html>
    '''
    def find_numbers(n):
    result = []
    for num in range(1, n + 1):
    digits = [int(d) for d in str(num)]
    product = 1
    for d in digits:
    product *= d
    if product > sum(digits):
    result.append(num)
    return result
    @app.route("/", methods=["GET", "POST"])
    def index():
    result = None
    if 网页链接 == "POST":
    n = int(request.form["n"])
    result = find_numbers(n)
    return render_template_string(template, result=result)
    if __name__ == "__main__":
    app.run(debug=True)


    IP属地:江苏来自Android客户端2楼2024-10-29 13:43
    回复


      IP属地:江苏来自Android客户端3楼2024-10-29 13:43
      回复
        简单点,如下
        def find_numbers(n):
        result = []
        for num in range(1, n + 1):
        digits = [int(d) for d in str(num)]
        product = 1
        for d in digits:
        product *= d
        if product > sum(digits):
        result.append(num)
        return result
        # 循环输入,直到用户输入有效的整数
        while True:
        try:
        n = int(input("请输入一个整数 n:"))
        print("满足条件的数字列表为:", find_numbers(n))
        break # 输入有效,跳出循环
        except ValueError:
        print("输入无效,请输入一个有效的整数!")
        # 等待用户按回车键退出
        input("按回车键退出程序...")


        IP属地:江苏来自Android客户端5楼2024-10-29 13:58
        回复