TypeError:传递给方法的不支持的格式字符串.__ format __

时间:2018-04-14 07:47:59

标签: python

我收到如下错误消息:TypeError:传递给方法.__ 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 %}

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

1 个答案:

答案 0 :(得分:0)

您需要通过调用方法设置值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>>