Can't make this random number generator to work properly












-1















I'm trying to make a random number generator and return the random generated number, but this code returns all the numbers before the random number. How can I return only the last string printed?



import random

from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))

for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')


Output for from_num = 0 and to_num = 20 by exemple, instead of '11' can return any number between these two given.



Random number: 0
Random number: 1
Random number: 2
Random number: 3
Random number: 4
Random number: 5
Random number: 6
Random number: 7
Random number: 8
Random number: 9
Random number: 10
Random number: 11









share|improve this question




















  • 3





    If you only want a single value, why on earth do you have a for loop?

    – jonrsharpe
    Dec 29 '18 at 18:58






  • 1





    Or on any planet for that matter.

    – Paritosh Singh
    Dec 29 '18 at 19:01






  • 1





    Also, your else clause will never get executed with True as your if statement condition.

    – busybear
    Dec 29 '18 at 19:02
















-1















I'm trying to make a random number generator and return the random generated number, but this code returns all the numbers before the random number. How can I return only the last string printed?



import random

from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))

for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')


Output for from_num = 0 and to_num = 20 by exemple, instead of '11' can return any number between these two given.



Random number: 0
Random number: 1
Random number: 2
Random number: 3
Random number: 4
Random number: 5
Random number: 6
Random number: 7
Random number: 8
Random number: 9
Random number: 10
Random number: 11









share|improve this question




















  • 3





    If you only want a single value, why on earth do you have a for loop?

    – jonrsharpe
    Dec 29 '18 at 18:58






  • 1





    Or on any planet for that matter.

    – Paritosh Singh
    Dec 29 '18 at 19:01






  • 1





    Also, your else clause will never get executed with True as your if statement condition.

    – busybear
    Dec 29 '18 at 19:02














-1












-1








-1


0






I'm trying to make a random number generator and return the random generated number, but this code returns all the numbers before the random number. How can I return only the last string printed?



import random

from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))

for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')


Output for from_num = 0 and to_num = 20 by exemple, instead of '11' can return any number between these two given.



Random number: 0
Random number: 1
Random number: 2
Random number: 3
Random number: 4
Random number: 5
Random number: 6
Random number: 7
Random number: 8
Random number: 9
Random number: 10
Random number: 11









share|improve this question
















I'm trying to make a random number generator and return the random generated number, but this code returns all the numbers before the random number. How can I return only the last string printed?



import random

from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))

for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')


Output for from_num = 0 and to_num = 20 by exemple, instead of '11' can return any number between these two given.



Random number: 0
Random number: 1
Random number: 2
Random number: 3
Random number: 4
Random number: 5
Random number: 6
Random number: 7
Random number: 8
Random number: 9
Random number: 10
Random number: 11






python loops random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 19:24







Isaak Schimdt

















asked Dec 29 '18 at 18:56









Isaak SchimdtIsaak Schimdt

82




82








  • 3





    If you only want a single value, why on earth do you have a for loop?

    – jonrsharpe
    Dec 29 '18 at 18:58






  • 1





    Or on any planet for that matter.

    – Paritosh Singh
    Dec 29 '18 at 19:01






  • 1





    Also, your else clause will never get executed with True as your if statement condition.

    – busybear
    Dec 29 '18 at 19:02














  • 3





    If you only want a single value, why on earth do you have a for loop?

    – jonrsharpe
    Dec 29 '18 at 18:58






  • 1





    Or on any planet for that matter.

    – Paritosh Singh
    Dec 29 '18 at 19:01






  • 1





    Also, your else clause will never get executed with True as your if statement condition.

    – busybear
    Dec 29 '18 at 19:02








3




3





If you only want a single value, why on earth do you have a for loop?

– jonrsharpe
Dec 29 '18 at 18:58





If you only want a single value, why on earth do you have a for loop?

– jonrsharpe
Dec 29 '18 at 18:58




1




1





Or on any planet for that matter.

– Paritosh Singh
Dec 29 '18 at 19:01





Or on any planet for that matter.

– Paritosh Singh
Dec 29 '18 at 19:01




1




1





Also, your else clause will never get executed with True as your if statement condition.

– busybear
Dec 29 '18 at 19:02





Also, your else clause will never get executed with True as your if statement condition.

– busybear
Dec 29 '18 at 19:02












3 Answers
3






active

oldest

votes


















1














Following to the comments above, just print the random value, without iterating on anything:



import random

from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))

if from_num > to_num:
print('You did not entered valid min/max numbers')
return

random_num = random.randrange(from_num,to_num+1):
print(f'Random number: {random_num}')





share|improve this answer


























  • This is not a complete solution as it drops the range check in the original example.

    – Jason Baumgartner
    Dec 29 '18 at 19:05






  • 1





    @JasonBaumgartner fixed, thanks

    – ItayB
    Dec 29 '18 at 19:09





















0














Replace this :



for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')


with :



ran_num = random.randint(from_num,to_num)
print("Random number is " + str(ran_num))





share|improve this answer































    0














    Why do you have a loop?



    import random

    from_num = int(input('Generate a random number:nFrom:'))
    to_num = int(input('To:'))

    if to_num > from_num:
    ran_number = random.randrange(from_num,to_num+1)
    print(f'Random number: {ran_number}')
    else:
    print('You did not entered valid min/max numbers')





    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',
      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%2f53972483%2fcant-make-this-random-number-generator-to-work-properly%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Following to the comments above, just print the random value, without iterating on anything:



      import random

      from_num = int(input('Generate a random number:nFrom:'))
      to_num = int(input('To:'))

      if from_num > to_num:
      print('You did not entered valid min/max numbers')
      return

      random_num = random.randrange(from_num,to_num+1):
      print(f'Random number: {random_num}')





      share|improve this answer


























      • This is not a complete solution as it drops the range check in the original example.

        – Jason Baumgartner
        Dec 29 '18 at 19:05






      • 1





        @JasonBaumgartner fixed, thanks

        – ItayB
        Dec 29 '18 at 19:09


















      1














      Following to the comments above, just print the random value, without iterating on anything:



      import random

      from_num = int(input('Generate a random number:nFrom:'))
      to_num = int(input('To:'))

      if from_num > to_num:
      print('You did not entered valid min/max numbers')
      return

      random_num = random.randrange(from_num,to_num+1):
      print(f'Random number: {random_num}')





      share|improve this answer


























      • This is not a complete solution as it drops the range check in the original example.

        – Jason Baumgartner
        Dec 29 '18 at 19:05






      • 1





        @JasonBaumgartner fixed, thanks

        – ItayB
        Dec 29 '18 at 19:09
















      1












      1








      1







      Following to the comments above, just print the random value, without iterating on anything:



      import random

      from_num = int(input('Generate a random number:nFrom:'))
      to_num = int(input('To:'))

      if from_num > to_num:
      print('You did not entered valid min/max numbers')
      return

      random_num = random.randrange(from_num,to_num+1):
      print(f'Random number: {random_num}')





      share|improve this answer















      Following to the comments above, just print the random value, without iterating on anything:



      import random

      from_num = int(input('Generate a random number:nFrom:'))
      to_num = int(input('To:'))

      if from_num > to_num:
      print('You did not entered valid min/max numbers')
      return

      random_num = random.randrange(from_num,to_num+1):
      print(f'Random number: {random_num}')






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 29 '18 at 19:08

























      answered Dec 29 '18 at 19:01









      ItayBItayB

      3,07042644




      3,07042644













      • This is not a complete solution as it drops the range check in the original example.

        – Jason Baumgartner
        Dec 29 '18 at 19:05






      • 1





        @JasonBaumgartner fixed, thanks

        – ItayB
        Dec 29 '18 at 19:09





















      • This is not a complete solution as it drops the range check in the original example.

        – Jason Baumgartner
        Dec 29 '18 at 19:05






      • 1





        @JasonBaumgartner fixed, thanks

        – ItayB
        Dec 29 '18 at 19:09



















      This is not a complete solution as it drops the range check in the original example.

      – Jason Baumgartner
      Dec 29 '18 at 19:05





      This is not a complete solution as it drops the range check in the original example.

      – Jason Baumgartner
      Dec 29 '18 at 19:05




      1




      1





      @JasonBaumgartner fixed, thanks

      – ItayB
      Dec 29 '18 at 19:09







      @JasonBaumgartner fixed, thanks

      – ItayB
      Dec 29 '18 at 19:09















      0














      Replace this :



      for num in range(random.randrange(from_num,to_num+1)):
      if True:
      print(f'Random number: {num}')
      else:
      print('You did not entered valid min/max numbers')


      with :



      ran_num = random.randint(from_num,to_num)
      print("Random number is " + str(ran_num))





      share|improve this answer




























        0














        Replace this :



        for num in range(random.randrange(from_num,to_num+1)):
        if True:
        print(f'Random number: {num}')
        else:
        print('You did not entered valid min/max numbers')


        with :



        ran_num = random.randint(from_num,to_num)
        print("Random number is " + str(ran_num))





        share|improve this answer


























          0












          0








          0







          Replace this :



          for num in range(random.randrange(from_num,to_num+1)):
          if True:
          print(f'Random number: {num}')
          else:
          print('You did not entered valid min/max numbers')


          with :



          ran_num = random.randint(from_num,to_num)
          print("Random number is " + str(ran_num))





          share|improve this answer













          Replace this :



          for num in range(random.randrange(from_num,to_num+1)):
          if True:
          print(f'Random number: {num}')
          else:
          print('You did not entered valid min/max numbers')


          with :



          ran_num = random.randint(from_num,to_num)
          print("Random number is " + str(ran_num))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 29 '18 at 19:02









          anirudhanirudh

          113




          113























              0














              Why do you have a loop?



              import random

              from_num = int(input('Generate a random number:nFrom:'))
              to_num = int(input('To:'))

              if to_num > from_num:
              ran_number = random.randrange(from_num,to_num+1)
              print(f'Random number: {ran_number}')
              else:
              print('You did not entered valid min/max numbers')





              share|improve this answer




























                0














                Why do you have a loop?



                import random

                from_num = int(input('Generate a random number:nFrom:'))
                to_num = int(input('To:'))

                if to_num > from_num:
                ran_number = random.randrange(from_num,to_num+1)
                print(f'Random number: {ran_number}')
                else:
                print('You did not entered valid min/max numbers')





                share|improve this answer


























                  0












                  0








                  0







                  Why do you have a loop?



                  import random

                  from_num = int(input('Generate a random number:nFrom:'))
                  to_num = int(input('To:'))

                  if to_num > from_num:
                  ran_number = random.randrange(from_num,to_num+1)
                  print(f'Random number: {ran_number}')
                  else:
                  print('You did not entered valid min/max numbers')





                  share|improve this answer













                  Why do you have a loop?



                  import random

                  from_num = int(input('Generate a random number:nFrom:'))
                  to_num = int(input('To:'))

                  if to_num > from_num:
                  ran_number = random.randrange(from_num,to_num+1)
                  print(f'Random number: {ran_number}')
                  else:
                  print('You did not entered valid min/max numbers')






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 29 '18 at 19:04









                  Jason BaumgartnerJason Baumgartner

                  292110




                  292110






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972483%2fcant-make-this-random-number-generator-to-work-properly%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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas