在 URL 端点内的 Flask 应用程序中显示小于或等于输入数字的斐波那契数列

Display Fibonnaci Sequence less than or equal to an input number in a flask app within a URL endpoint

我正在使用烧瓶应用程序和 url 端点来允许输入数字。然后我想显示斐波纳契序列直到它等于或小于输入的数字。

这是我目前拥有的:

@app.route("/fibonacci/<int:param_fi>/")
def getFib(param_fi):
if param_fi < 2:
    return ('0,1,1')
else:
    L = getFib(param_fi-1)
    if L[-1] < param_fi:
        L.append(L[-1] + L[-2])
    return L

我无法准确指出错误的来源。我曾尝试制作一个列表并将其转换为字符串,但始终无法正常工作。当我尝试这样做时 returns 出现以下错误: "The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a list."

我正在寻找以下输出:

/fibonacci/250(这是用户输入)/

0,1,1,2,3,5,8,13,21,34,55,89,144,233

或/fibonacci/90(这是用户输入)/

0,1,1,2,3,5,8,13,21,34,55,89

感谢任何帮助。

决赛

@app.route("/fibonacci/<int:param_fi>/")
def getFib(param_fi):
    i = 0
    j = 1
    sequence = []
    current_operation = 0
    index = 0
    while True:
        sequence.append(i)
        current_operation = i + j
        i = j
        j = current_operation
        if i > param_fi:
            return json.dumps(sequence)
        else:
            index += 1
    return json.dumps(sequence)

我没有理解错误,你能输出你想要的结果吗?您是否需要像以前那样递归?

但我猜你漏掉了什么?你只有 return 0 或 1,或两者之和,所以是的,你永远不会有完整的斐波那契数列。

您至少需要在内存中保存序列,或 return 列表并每次添加元素。

编辑 https://repl.it/@skapin/AcceptableFoolishAssemblylanguage

def fibo(params):
  i = 0
  j = 1
  sequence = []
  current_operation = 0
  for current_n in range(0, params+1):
    # We appends now, since f(0) = 0 = i_initial , f(1) = 1 =j_initial
    sequence.append(i)
    # prepare next opération
    current_operation = i + j
    i = j
    j = current_operation

  return sequence

print(fibo(10))

EDIT2-Flask

from flask import jsonify

@app.route("/fibonacci/<int:param_fi>/")
def get_fibo(param_fi):
    return jsonify(fibo(param_fi))

决赛

from flask import jsonify
def fibo(params):
  i = 0
  j = 1
  sequence = []
  current_operation = 0
  index = 0
  while True:
    # We appends now, since f(0) = 0 = i_initial , f(1) = 1 =j_initial
    sequence.append(i)
    # prepare next opération
    current_operation = i + j
    i = j
    j = current_operation
    # Stop condition
    if i > params:
      return sequence
    else:
      index += 1

  return sequence


@app.route("/fibonacci/<int:param_fi>/")
def get_fibo(param_fi):
    return jsonify(fibo(param_fi))