I want to apply loop but i don't know how to apply it on my code











up vote
0
down vote

favorite












here's my code



#Program for calculating the nTH Term
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)


how can i make this code to run again using if condition and with new values which i will input again ORrr i just have to put an if condition and copy/paste my code inside it?










share|improve this question




















  • 1




    inside a while loop?
    – mad_
    Nov 8 at 16:53















up vote
0
down vote

favorite












here's my code



#Program for calculating the nTH Term
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)


how can i make this code to run again using if condition and with new values which i will input again ORrr i just have to put an if condition and copy/paste my code inside it?










share|improve this question




















  • 1




    inside a while loop?
    – mad_
    Nov 8 at 16:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











here's my code



#Program for calculating the nTH Term
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)


how can i make this code to run again using if condition and with new values which i will input again ORrr i just have to put an if condition and copy/paste my code inside it?










share|improve this question















here's my code



#Program for calculating the nTH Term
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)


how can i make this code to run again using if condition and with new values which i will input again ORrr i just have to put an if condition and copy/paste my code inside it?







python loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 16:54

























asked Nov 8 at 16:52









AbdurRafay

112




112








  • 1




    inside a while loop?
    – mad_
    Nov 8 at 16:53














  • 1




    inside a while loop?
    – mad_
    Nov 8 at 16:53








1




1




inside a while loop?
– mad_
Nov 8 at 16:53




inside a while loop?
– mad_
Nov 8 at 16:53












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










while True:
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)


This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point






share|improve this answer




























    up vote
    0
    down vote













    I would try something like this:



    def inputs():
    a=int(input("The first term is:"))
    d=int(input("The common difference is:"))
    return (a, d)

    def nth_term():
    a, d = inputs()
    sequence=range(0,40,3)
    n=len(sequence)
    print()
    print("The number of terms is:",n)
    print("The last term of sequence is: ")
    Tn=a+((n-1)*d)
    print(Tn)


    Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:



    for each in range(5):
    nth_term()





    share|improve this answer





















      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',
      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%2f53212502%2fi-want-to-apply-loop-but-i-dont-know-how-to-apply-it-on-my-code%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      while True:
      sequence=range(0,40,3)
      a=int(input("The first term is:"))
      d=int(input("The common difference is:"))
      n=len(sequence)
      print("The number of terms is:",n)
      print("The last term of sequence is: ")
      Tn=a+((n-1)*d)
      print(Tn)


      This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point






      share|improve this answer

























        up vote
        0
        down vote



        accepted










        while True:
        sequence=range(0,40,3)
        a=int(input("The first term is:"))
        d=int(input("The common difference is:"))
        n=len(sequence)
        print("The number of terms is:",n)
        print("The last term of sequence is: ")
        Tn=a+((n-1)*d)
        print(Tn)


        This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point






        share|improve this answer























          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          while True:
          sequence=range(0,40,3)
          a=int(input("The first term is:"))
          d=int(input("The common difference is:"))
          n=len(sequence)
          print("The number of terms is:",n)
          print("The last term of sequence is: ")
          Tn=a+((n-1)*d)
          print(Tn)


          This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point






          share|improve this answer












          while True:
          sequence=range(0,40,3)
          a=int(input("The first term is:"))
          d=int(input("The common difference is:"))
          n=len(sequence)
          print("The number of terms is:",n)
          print("The last term of sequence is: ")
          Tn=a+((n-1)*d)
          print(Tn)


          This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 16:58









          Karl

          1,85842951




          1,85842951
























              up vote
              0
              down vote













              I would try something like this:



              def inputs():
              a=int(input("The first term is:"))
              d=int(input("The common difference is:"))
              return (a, d)

              def nth_term():
              a, d = inputs()
              sequence=range(0,40,3)
              n=len(sequence)
              print()
              print("The number of terms is:",n)
              print("The last term of sequence is: ")
              Tn=a+((n-1)*d)
              print(Tn)


              Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:



              for each in range(5):
              nth_term()





              share|improve this answer

























                up vote
                0
                down vote













                I would try something like this:



                def inputs():
                a=int(input("The first term is:"))
                d=int(input("The common difference is:"))
                return (a, d)

                def nth_term():
                a, d = inputs()
                sequence=range(0,40,3)
                n=len(sequence)
                print()
                print("The number of terms is:",n)
                print("The last term of sequence is: ")
                Tn=a+((n-1)*d)
                print(Tn)


                Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:



                for each in range(5):
                nth_term()





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I would try something like this:



                  def inputs():
                  a=int(input("The first term is:"))
                  d=int(input("The common difference is:"))
                  return (a, d)

                  def nth_term():
                  a, d = inputs()
                  sequence=range(0,40,3)
                  n=len(sequence)
                  print()
                  print("The number of terms is:",n)
                  print("The last term of sequence is: ")
                  Tn=a+((n-1)*d)
                  print(Tn)


                  Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:



                  for each in range(5):
                  nth_term()





                  share|improve this answer












                  I would try something like this:



                  def inputs():
                  a=int(input("The first term is:"))
                  d=int(input("The common difference is:"))
                  return (a, d)

                  def nth_term():
                  a, d = inputs()
                  sequence=range(0,40,3)
                  n=len(sequence)
                  print()
                  print("The number of terms is:",n)
                  print("The last term of sequence is: ")
                  Tn=a+((n-1)*d)
                  print(Tn)


                  Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:



                  for each in range(5):
                  nth_term()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 17:12









                  John Meade

                  444




                  444






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212502%2fi-want-to-apply-loop-but-i-dont-know-how-to-apply-it-on-my-code%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

                      How to pass form data using jquery Ajax to insert data in database?

                      National Museum of Racing and Hall of Fame

                      Guess what letter conforming each word