Time delay indication bar for a text game

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
7
down vote

favorite
1












For a game I am writing in Python 3.6.5, I made a text-based animation to indicate the passage of time (when chopping a tree or mining a rock, etc). I did a lot of trial and error to get the output how I wanted it, however it seems like I went about it in an overly-complicated way. I was wondering if there is a more efficient way to write this.



import os
import time


def progress_bar(text,secs):
bar = '['
c = 0 #used in sum method to determine number of Xs in bar

for i in range(0,11):
print(text+'n')
bar = bar.replace(' ','') #get rid of previous spaces added to bar
num_of_X = sum(c == 'X' for c in bar) #get number of Xs (progress)
for x in range(0,(10 - num_of_X)): #Fill remaining space after Xs
bar += ' '
print(bar + ']')
bar += 'X'
time.sleep(secs)
os.system('cls' if os.name == 'nt' else 'clear') #clears terminal

input('Loading complete!')

progress_bar('Enter Your Text Here!',0.15)






share|improve this question



























    up vote
    7
    down vote

    favorite
    1












    For a game I am writing in Python 3.6.5, I made a text-based animation to indicate the passage of time (when chopping a tree or mining a rock, etc). I did a lot of trial and error to get the output how I wanted it, however it seems like I went about it in an overly-complicated way. I was wondering if there is a more efficient way to write this.



    import os
    import time


    def progress_bar(text,secs):
    bar = '['
    c = 0 #used in sum method to determine number of Xs in bar

    for i in range(0,11):
    print(text+'n')
    bar = bar.replace(' ','') #get rid of previous spaces added to bar
    num_of_X = sum(c == 'X' for c in bar) #get number of Xs (progress)
    for x in range(0,(10 - num_of_X)): #Fill remaining space after Xs
    bar += ' '
    print(bar + ']')
    bar += 'X'
    time.sleep(secs)
    os.system('cls' if os.name == 'nt' else 'clear') #clears terminal

    input('Loading complete!')

    progress_bar('Enter Your Text Here!',0.15)






    share|improve this question























      up vote
      7
      down vote

      favorite
      1









      up vote
      7
      down vote

      favorite
      1






      1





      For a game I am writing in Python 3.6.5, I made a text-based animation to indicate the passage of time (when chopping a tree or mining a rock, etc). I did a lot of trial and error to get the output how I wanted it, however it seems like I went about it in an overly-complicated way. I was wondering if there is a more efficient way to write this.



      import os
      import time


      def progress_bar(text,secs):
      bar = '['
      c = 0 #used in sum method to determine number of Xs in bar

      for i in range(0,11):
      print(text+'n')
      bar = bar.replace(' ','') #get rid of previous spaces added to bar
      num_of_X = sum(c == 'X' for c in bar) #get number of Xs (progress)
      for x in range(0,(10 - num_of_X)): #Fill remaining space after Xs
      bar += ' '
      print(bar + ']')
      bar += 'X'
      time.sleep(secs)
      os.system('cls' if os.name == 'nt' else 'clear') #clears terminal

      input('Loading complete!')

      progress_bar('Enter Your Text Here!',0.15)






      share|improve this question













      For a game I am writing in Python 3.6.5, I made a text-based animation to indicate the passage of time (when chopping a tree or mining a rock, etc). I did a lot of trial and error to get the output how I wanted it, however it seems like I went about it in an overly-complicated way. I was wondering if there is a more efficient way to write this.



      import os
      import time


      def progress_bar(text,secs):
      bar = '['
      c = 0 #used in sum method to determine number of Xs in bar

      for i in range(0,11):
      print(text+'n')
      bar = bar.replace(' ','') #get rid of previous spaces added to bar
      num_of_X = sum(c == 'X' for c in bar) #get number of Xs (progress)
      for x in range(0,(10 - num_of_X)): #Fill remaining space after Xs
      bar += ' '
      print(bar + ']')
      bar += 'X'
      time.sleep(secs)
      os.system('cls' if os.name == 'nt' else 'clear') #clears terminal

      input('Loading complete!')

      progress_bar('Enter Your Text Here!',0.15)








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 22 at 20:35
























      asked May 22 at 19:59









      ShroomBandit

      1385




      1385




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          1. You can use bar.count rather than your sum

          2. You can use ' ' * n, rather than your for loop.

          3. You don't need [ in your bar.


          4. You don't need to add whitespace, str.format can do that for you.



            >>> '[:<10]'.format('XX')
            '[XX ]'


          5. You don't need the bar variable.


          6. I'd move the input out of the function, as it doesn't have much to do with the bar.

          7. You could change the size of the bar with another argument.

          def progress_bar(text, secs, size=10):
          for i in range(size + 1):
          print('nn[:<]'.format(text, 'X' * i, size))
          time.sleep(secs)
          os.system('cls' if os.name == 'nt' else 'clear')



          You can also remove the need for os if your terminal support r. This will mean that you can remove text from the function too, as IMO it shouldn't really be there either.



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('r[:<]'.format('X' * i, size), end='', flush=True)
          time.sleep(secs)
          print()

          print('Your text here!n')
          progress_bar(0.15)
          input('>')


          Alternately you can use b, this is better if you want to allow text like bar: .



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('[:<]'.format('X' * i, size, 'b' * (size + 2)), end='', flush=True)
          time.sleep(secs)
          print()

          print('bar: ', end='', flush=True)
          progress_bar(0.15)
          input('>')





          share|improve this answer























          • I need to learn more about string formatting as you've shown me. I will dive into it now, very helpful answer. Thank you!
            – ShroomBandit
            May 22 at 20:34










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194972%2ftime-delay-indication-bar-for-a-text-game%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          1. You can use bar.count rather than your sum

          2. You can use ' ' * n, rather than your for loop.

          3. You don't need [ in your bar.


          4. You don't need to add whitespace, str.format can do that for you.



            >>> '[:<10]'.format('XX')
            '[XX ]'


          5. You don't need the bar variable.


          6. I'd move the input out of the function, as it doesn't have much to do with the bar.

          7. You could change the size of the bar with another argument.

          def progress_bar(text, secs, size=10):
          for i in range(size + 1):
          print('nn[:<]'.format(text, 'X' * i, size))
          time.sleep(secs)
          os.system('cls' if os.name == 'nt' else 'clear')



          You can also remove the need for os if your terminal support r. This will mean that you can remove text from the function too, as IMO it shouldn't really be there either.



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('r[:<]'.format('X' * i, size), end='', flush=True)
          time.sleep(secs)
          print()

          print('Your text here!n')
          progress_bar(0.15)
          input('>')


          Alternately you can use b, this is better if you want to allow text like bar: .



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('[:<]'.format('X' * i, size, 'b' * (size + 2)), end='', flush=True)
          time.sleep(secs)
          print()

          print('bar: ', end='', flush=True)
          progress_bar(0.15)
          input('>')





          share|improve this answer























          • I need to learn more about string formatting as you've shown me. I will dive into it now, very helpful answer. Thank you!
            – ShroomBandit
            May 22 at 20:34














          up vote
          6
          down vote



          accepted










          1. You can use bar.count rather than your sum

          2. You can use ' ' * n, rather than your for loop.

          3. You don't need [ in your bar.


          4. You don't need to add whitespace, str.format can do that for you.



            >>> '[:<10]'.format('XX')
            '[XX ]'


          5. You don't need the bar variable.


          6. I'd move the input out of the function, as it doesn't have much to do with the bar.

          7. You could change the size of the bar with another argument.

          def progress_bar(text, secs, size=10):
          for i in range(size + 1):
          print('nn[:<]'.format(text, 'X' * i, size))
          time.sleep(secs)
          os.system('cls' if os.name == 'nt' else 'clear')



          You can also remove the need for os if your terminal support r. This will mean that you can remove text from the function too, as IMO it shouldn't really be there either.



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('r[:<]'.format('X' * i, size), end='', flush=True)
          time.sleep(secs)
          print()

          print('Your text here!n')
          progress_bar(0.15)
          input('>')


          Alternately you can use b, this is better if you want to allow text like bar: .



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('[:<]'.format('X' * i, size, 'b' * (size + 2)), end='', flush=True)
          time.sleep(secs)
          print()

          print('bar: ', end='', flush=True)
          progress_bar(0.15)
          input('>')





          share|improve this answer























          • I need to learn more about string formatting as you've shown me. I will dive into it now, very helpful answer. Thank you!
            – ShroomBandit
            May 22 at 20:34












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          1. You can use bar.count rather than your sum

          2. You can use ' ' * n, rather than your for loop.

          3. You don't need [ in your bar.


          4. You don't need to add whitespace, str.format can do that for you.



            >>> '[:<10]'.format('XX')
            '[XX ]'


          5. You don't need the bar variable.


          6. I'd move the input out of the function, as it doesn't have much to do with the bar.

          7. You could change the size of the bar with another argument.

          def progress_bar(text, secs, size=10):
          for i in range(size + 1):
          print('nn[:<]'.format(text, 'X' * i, size))
          time.sleep(secs)
          os.system('cls' if os.name == 'nt' else 'clear')



          You can also remove the need for os if your terminal support r. This will mean that you can remove text from the function too, as IMO it shouldn't really be there either.



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('r[:<]'.format('X' * i, size), end='', flush=True)
          time.sleep(secs)
          print()

          print('Your text here!n')
          progress_bar(0.15)
          input('>')


          Alternately you can use b, this is better if you want to allow text like bar: .



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('[:<]'.format('X' * i, size, 'b' * (size + 2)), end='', flush=True)
          time.sleep(secs)
          print()

          print('bar: ', end='', flush=True)
          progress_bar(0.15)
          input('>')





          share|improve this answer















          1. You can use bar.count rather than your sum

          2. You can use ' ' * n, rather than your for loop.

          3. You don't need [ in your bar.


          4. You don't need to add whitespace, str.format can do that for you.



            >>> '[:<10]'.format('XX')
            '[XX ]'


          5. You don't need the bar variable.


          6. I'd move the input out of the function, as it doesn't have much to do with the bar.

          7. You could change the size of the bar with another argument.

          def progress_bar(text, secs, size=10):
          for i in range(size + 1):
          print('nn[:<]'.format(text, 'X' * i, size))
          time.sleep(secs)
          os.system('cls' if os.name == 'nt' else 'clear')



          You can also remove the need for os if your terminal support r. This will mean that you can remove text from the function too, as IMO it shouldn't really be there either.



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('r[:<]'.format('X' * i, size), end='', flush=True)
          time.sleep(secs)
          print()

          print('Your text here!n')
          progress_bar(0.15)
          input('>')


          Alternately you can use b, this is better if you want to allow text like bar: .



          def progress_bar(secs, size=10):
          for i in range(size + 1):
          print('[:<]'.format('X' * i, size, 'b' * (size + 2)), end='', flush=True)
          time.sleep(secs)
          print()

          print('bar: ', end='', flush=True)
          progress_bar(0.15)
          input('>')






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited May 22 at 20:37


























          answered May 22 at 20:18









          Peilonrayz

          24.3k336102




          24.3k336102











          • I need to learn more about string formatting as you've shown me. I will dive into it now, very helpful answer. Thank you!
            – ShroomBandit
            May 22 at 20:34
















          • I need to learn more about string formatting as you've shown me. I will dive into it now, very helpful answer. Thank you!
            – ShroomBandit
            May 22 at 20:34















          I need to learn more about string formatting as you've shown me. I will dive into it now, very helpful answer. Thank you!
          – ShroomBandit
          May 22 at 20:34




          I need to learn more about string formatting as you've shown me. I will dive into it now, very helpful answer. Thank you!
          – ShroomBandit
          May 22 at 20:34












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194972%2ftime-delay-indication-bar-for-a-text-game%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation