【问题标题】:TypeError: unsupported format string passed to method.__format__TypeError:不支持的格式字符串传递给 method.__format__
【发布时间】:2018-04-14 07:47:59
【问题描述】:

我收到如下错误消息:TypeError: unsupported format string passing to method.__format __.

我使用的是 Linux 环境,这段代码应该计算利率。有单独的文件用于运行脚本和函数。但是,一旦在页面中输入数据,我就会在页面上看到一条错误消息,指出以下代码之前的格式不正确:

return render_template('interest_form.html', totale="{0:.2f}".format(totale))

以下是完整代码:

from server import app, valid_time
from flask import request, render_template
from Calculator import Calculator


@app.route("/", methods=["POST", "GET"])
def interest_total():
    if request.method == 'POST':
        initial=float(request.form["initial"])
        rate=float(request.form["rate"])
        time=float(request.form["time"])
        data=Calculator(initial,rate)
        data.total_interest(time)
        totale=data.total_interest
        return render_template('interest_form.html', totale="{0:.2f}".format(totale))
    return render_template('interest_form.html', calc_total=True)


@app.route('/time', methods=["POST", "GET"])
def time_interest():
    if request.method == 'POST':
        initial=float(request.form["initial"])
        rate=float(request.form["rate"])
        total=float(request.form["total"])
        dataa=Calculator(initial,rate)
        dataa.time_required(total)
        time=dataa.time_required
        return render_template('interest_form.html', time="{0:.2f}".format(time))
    return render_template('interest_form.html', calc_time=True)



@app.route('/credits', methods=['GET'])
def credits():
return render_template('credits.html')

这是计算器部分的代码:

class Calculator:
    def __init__(self, initial, rate):

        """
        Pass in the initial amount invested
        and the rate as a percentage (ie 50 for 50%)
        """
        self._initial = initial
        self._rate = rate/100

    def total_interest(self, time):
        """
        Returns the total amoun of interest after a given length of time
        """
        return self._initial * self._rate * time

    def time_required(self, total):
        """
        Returns the length of time required to achieve a given total
        """
        return total/(self._initial * self._rate)

我正在尝试使用 html 表单来发送输入:

{% extends 'base.html' %}
{% block content %}

<html>
<body>
<div>
    <form action="" method='POST'>
         <div style="margin: 10px 0px">
            <label>Amount Invested ($): </label><br/>
            <input  name="initial" input type="number" step="0.01" placeholder="Amount Invested"/>
        </div>
         <div style="margin: 10px 0px">
            <label>Interest Rate (%): </label><br/>
            <input  name="rate" input type="number" step="0.01" placeholder="Amount Invested"/>
        </div>
         <div style="margin: 10px 0px">
            <label>Time Investment (Years): </label><br/>
            <input name="time" input type="number" step="0.01" placeholder="Amount Invested"  {%if calc_time %}disabled{% endif %}/>
        </div>
         <div style="margin: 10px 0px">
            <label>Total Interest ($): </label><br/>
            <input  name="total" input type="number" step="0.01" placeholder="Amount Invested"  {%if calc_total %}disabled{% endif %}/>
        </div>
        <div style="margin: 10px 0px">
            <button type="submit">Submit</button>
        </div>

                {% if totale %}<h4>Total amount is</h4>
                <textarea name="Amount" rows="5" cols="20"> {{totale}}</textarea>
                {% endif %}
                </form>
        {% if time %}<h4>Total amount is</h4>
                <textarea name="Amount" rows="5" cols="20"> {{time}}</textarea>
                {% endif %}
                </form>
</div>
<div>
<a href="{{url_for('time_interest')}}">Time_Form</a>
<a href="{{url_for('interest_total')}}">Total Form</a>
<br/><a href="{{url_for('credits')}}">Credits Page</a>
</div>
</body>
</html>
{% endblock %}

我使用了一个不同的模板代码,它具有类似的功能和返回格式,运行它时似乎没有问题。然而,这似乎显示了上述错误消息。

【问题讨论】:

    标签: python


    【解决方案1】:

    你需要通过调用方法设置值totale,写

    totale=data.total_interest(time)
    #                         ^^^^^^
    

    而不是

    data.total_interest(time)
    totale=data.total_interest
    

    第二种情况也一样

    time = dataa.time_required(total)
    

    而不是

    dataa.time_required(total)
    time=dataa.time_require
    

    在你的定义中,当你调用方法时你得到一个值,当你访问方法时你得到描述,例如:

    In [14]: data.total_interest(time)
    Out[14]: 2.8000000000000003
    

    In [15]: data.total_interest
    Out[15]: <bound method Calculator.total_interest of <__main__.Calculator object at 0x7f3e4c575668>>
    

    【讨论】:

    • 谢谢它的工作!!浪费了大约 4 小时试图找出问题的根源。我猜我还是个初学者。如果我可以问这两行有什么区别?
    • 很高兴为您提供帮助
    猜你喜欢
    • 2017-12-30
    • 2021-10-31
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多