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












0














I am using a flask app and url endpoint to allow the input of a number. I then want to display the fibonnaci sequence up until it is equal to or less than the inputted number.



This is what I currently have:



@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


I am having trouble pinpointing exactly where the error is from. I have tried making a list and converting it to strings but can't ever get it to work. When I try this it returns the following error:
"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."



I am looking for the following output:



/fibonacci/250(this is the user input)/



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


Or /fibonacci/90(this is the user input)/



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


Any help is appreciated.



Final



@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)









share|improve this question





























    0














    I am using a flask app and url endpoint to allow the input of a number. I then want to display the fibonnaci sequence up until it is equal to or less than the inputted number.



    This is what I currently have:



    @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


    I am having trouble pinpointing exactly where the error is from. I have tried making a list and converting it to strings but can't ever get it to work. When I try this it returns the following error:
    "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."



    I am looking for the following output:



    /fibonacci/250(this is the user input)/



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


    Or /fibonacci/90(this is the user input)/



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


    Any help is appreciated.



    Final



    @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)









    share|improve this question



























      0












      0








      0







      I am using a flask app and url endpoint to allow the input of a number. I then want to display the fibonnaci sequence up until it is equal to or less than the inputted number.



      This is what I currently have:



      @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


      I am having trouble pinpointing exactly where the error is from. I have tried making a list and converting it to strings but can't ever get it to work. When I try this it returns the following error:
      "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."



      I am looking for the following output:



      /fibonacci/250(this is the user input)/



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


      Or /fibonacci/90(this is the user input)/



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


      Any help is appreciated.



      Final



      @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)









      share|improve this question















      I am using a flask app and url endpoint to allow the input of a number. I then want to display the fibonnaci sequence up until it is equal to or less than the inputted number.



      This is what I currently have:



      @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


      I am having trouble pinpointing exactly where the error is from. I have tried making a list and converting it to strings but can't ever get it to work. When I try this it returns the following error:
      "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."



      I am looking for the following output:



      /fibonacci/250(this is the user input)/



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


      Or /fibonacci/90(this is the user input)/



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


      Any help is appreciated.



      Final



      @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)






      python url flask endpoint






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 21:25

























      asked Nov 12 at 18:57









      Dakota Brown

      2716




      2716
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I did not understand the error, can you output the result you want to have ? Do you need recursive like you did or no ?



          But I guess you are missing something? You only return 0 or 1, or sum of both, so yes, you will never have the full sequence of the fibonacci.



          You need to keep in memory the sequence, or return a list and adding elements each time, at least.



          EDIT
          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))


          Final



          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))





          share|improve this answer



















          • 1




            tell me if it's okay to you I edited my answere
            – Skapin
            Nov 12 at 20:29








          • 1




            Did you try it online ?
            – Skapin
            Nov 12 at 20:38






          • 1




            on the code you past, you forgot the underscore between current/operation
            – Skapin
            Nov 12 at 20:39








          • 1




            So now, you need to know "how to return a list in flask". You can return a json for example, or a string of your list. Your choice
            – Skapin
            Nov 12 at 20:47








          • 1




            @DakotaBrown I edited my 1st response, test and tell us but it should be ok (but ain't tested it, no flask under my hand at the moment)
            – Skapin
            Nov 12 at 20:54













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268431%2fdisplay-fibonnaci-sequence-less-than-or-equal-to-an-input-number-in-a-flask-app%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I did not understand the error, can you output the result you want to have ? Do you need recursive like you did or no ?



          But I guess you are missing something? You only return 0 or 1, or sum of both, so yes, you will never have the full sequence of the fibonacci.



          You need to keep in memory the sequence, or return a list and adding elements each time, at least.



          EDIT
          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))


          Final



          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))





          share|improve this answer



















          • 1




            tell me if it's okay to you I edited my answere
            – Skapin
            Nov 12 at 20:29








          • 1




            Did you try it online ?
            – Skapin
            Nov 12 at 20:38






          • 1




            on the code you past, you forgot the underscore between current/operation
            – Skapin
            Nov 12 at 20:39








          • 1




            So now, you need to know "how to return a list in flask". You can return a json for example, or a string of your list. Your choice
            – Skapin
            Nov 12 at 20:47








          • 1




            @DakotaBrown I edited my 1st response, test and tell us but it should be ok (but ain't tested it, no flask under my hand at the moment)
            – Skapin
            Nov 12 at 20:54


















          1














          I did not understand the error, can you output the result you want to have ? Do you need recursive like you did or no ?



          But I guess you are missing something? You only return 0 or 1, or sum of both, so yes, you will never have the full sequence of the fibonacci.



          You need to keep in memory the sequence, or return a list and adding elements each time, at least.



          EDIT
          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))


          Final



          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))





          share|improve this answer



















          • 1




            tell me if it's okay to you I edited my answere
            – Skapin
            Nov 12 at 20:29








          • 1




            Did you try it online ?
            – Skapin
            Nov 12 at 20:38






          • 1




            on the code you past, you forgot the underscore between current/operation
            – Skapin
            Nov 12 at 20:39








          • 1




            So now, you need to know "how to return a list in flask". You can return a json for example, or a string of your list. Your choice
            – Skapin
            Nov 12 at 20:47








          • 1




            @DakotaBrown I edited my 1st response, test and tell us but it should be ok (but ain't tested it, no flask under my hand at the moment)
            – Skapin
            Nov 12 at 20:54
















          1












          1








          1






          I did not understand the error, can you output the result you want to have ? Do you need recursive like you did or no ?



          But I guess you are missing something? You only return 0 or 1, or sum of both, so yes, you will never have the full sequence of the fibonacci.



          You need to keep in memory the sequence, or return a list and adding elements each time, at least.



          EDIT
          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))


          Final



          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))





          share|improve this answer














          I did not understand the error, can you output the result you want to have ? Do you need recursive like you did or no ?



          But I guess you are missing something? You only return 0 or 1, or sum of both, so yes, you will never have the full sequence of the fibonacci.



          You need to keep in memory the sequence, or return a list and adding elements each time, at least.



          EDIT
          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))


          Final



          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))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 21:04

























          answered Nov 12 at 19:49









          Skapin

          665




          665








          • 1




            tell me if it's okay to you I edited my answere
            – Skapin
            Nov 12 at 20:29








          • 1




            Did you try it online ?
            – Skapin
            Nov 12 at 20:38






          • 1




            on the code you past, you forgot the underscore between current/operation
            – Skapin
            Nov 12 at 20:39








          • 1




            So now, you need to know "how to return a list in flask". You can return a json for example, or a string of your list. Your choice
            – Skapin
            Nov 12 at 20:47








          • 1




            @DakotaBrown I edited my 1st response, test and tell us but it should be ok (but ain't tested it, no flask under my hand at the moment)
            – Skapin
            Nov 12 at 20:54
















          • 1




            tell me if it's okay to you I edited my answere
            – Skapin
            Nov 12 at 20:29








          • 1




            Did you try it online ?
            – Skapin
            Nov 12 at 20:38






          • 1




            on the code you past, you forgot the underscore between current/operation
            – Skapin
            Nov 12 at 20:39








          • 1




            So now, you need to know "how to return a list in flask". You can return a json for example, or a string of your list. Your choice
            – Skapin
            Nov 12 at 20:47








          • 1




            @DakotaBrown I edited my 1st response, test and tell us but it should be ok (but ain't tested it, no flask under my hand at the moment)
            – Skapin
            Nov 12 at 20:54










          1




          1




          tell me if it's okay to you I edited my answere
          – Skapin
          Nov 12 at 20:29






          tell me if it's okay to you I edited my answere
          – Skapin
          Nov 12 at 20:29






          1




          1




          Did you try it online ?
          – Skapin
          Nov 12 at 20:38




          Did you try it online ?
          – Skapin
          Nov 12 at 20:38




          1




          1




          on the code you past, you forgot the underscore between current/operation
          – Skapin
          Nov 12 at 20:39






          on the code you past, you forgot the underscore between current/operation
          – Skapin
          Nov 12 at 20:39






          1




          1




          So now, you need to know "how to return a list in flask". You can return a json for example, or a string of your list. Your choice
          – Skapin
          Nov 12 at 20:47






          So now, you need to know "how to return a list in flask". You can return a json for example, or a string of your list. Your choice
          – Skapin
          Nov 12 at 20:47






          1




          1




          @DakotaBrown I edited my 1st response, test and tell us but it should be ok (but ain't tested it, no flask under my hand at the moment)
          – Skapin
          Nov 12 at 20:54






          @DakotaBrown I edited my 1st response, test and tell us but it should be ok (but ain't tested it, no flask under my hand at the moment)
          – Skapin
          Nov 12 at 20:54




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268431%2fdisplay-fibonnaci-sequence-less-than-or-equal-to-an-input-number-in-a-flask-app%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python