Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Repeating a block of code
#1
Is there a command which will repeat a block of code a set number of time. eg 10.times do:

my code here
Reply
#2
You could put the code into a loop. (You may want to consider putting the repeated code into a function? but this is a seperate issue)
Then have a loop cntr around a while loop
After each iteration increase the cntr by 1
once the loop cntr reaches a target set in the while loop, the loop will be completed.

Is this the kind of thing that would work for you?

Bass

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#3
what you are looking for is loop
there are different code constructs, but based on your example, one very simple solution would be for-loop:

for i in range(10):
    print('something') # the body of the loop can be anything you want
this is simple loop, and the body will be executed 10 times. if you need you can use the control variable i in the body of the loop.

in other cases you can use while-loop
Reply
#4
Many thanks Bass and Buran for quick replies. I was was aware of the methods you suggested but I was under impression that I had seen code like this :- 10.Times do: but I cannot remember where. !!!!
Reply
#5
This could be a fragment of iterable slicing

a_list[:-10] returns all elements from the list to 10th before the end. For instance
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
I think this is what buran is suggesting.

This single line of code will repeat any code (indented by 4 spaces) below this line x10 times. Due to the way Python works, there is no need to close the loop with any code, the interpreter executes all indented code below the initial statement.

for x in range(10):
    Your code that you want repeated x10 should go here (note indentation)
    This line will also execute x10

This line is not indented and therefore will not execute x10
There may be other ways of doing this but this solution is short and in a single line.

Let me know if this is not what you want.

Good Luck

Bass

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#7
Read BBCODE
you need to put your code, errors and output in the proper code tags
Reply
#8
Bass basically answered this already, but I wanted to chime in and let you know what I did to my son a few days ago (I try to show my kids everything I learn in some fun way; perhaps spark a creative want for programming to see if it wants to stick with them). I called my son in and told him I can type "I am the world's greatest Dad!" so fast I can do it 1,000 times in only a few seconds. Wide-eyed and achin' to see me try, he told me to type it once. I pulled up IDLE and typed "I am the world's greatest Dad!" rather slowly.

After calculating mere seconds are already expired he called me on my bluff. What he did not realize, I had already typed this into IDLE:
p = "I am the world\'s greatest Dad!"
I pulled up my phone's timer and handed him my phone then told him to time me and let me know when to start. When he told me to go I typed:
for i in range(1000):
    print(p)
NOTE: Hit ENTER twice here after closing that last parenthesis.

As soon as I hit ENTER the second time I acted like I was typing so fast causing my fingers to rattle the keys to simulate me typing (when I wasn't). Oh the look on his face... it was so much fun. Poor kid didn't even stop the timer. After about 15 seconds he set the phone down, politely said, "Cheater!" and walked away. I had way too much fun with that one. You guys and gals should have seen it.
Reply
#9
(Jun-11-2017, 03:57 PM)owdcoder Wrote: I was under impression that I had seen code like this :- 10.Times do: but I cannot remember where. !!!!

You probably have, but that's not Python, that's Ruby.

https://ruby-doc.org/core-2.2.0/Integer....od-i-times Wrote:times {|i| block } → self click to toggle source
times → an_enumerator

Iterates the given block int times, passing in values from zero to int - 1.

If no block is given, an Enumerator is returned instead.
5.times do |i|
  print i, " "
end
#=> 0 1 2 3 4
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 643 Dec-12-2023, 09:09 AM
Last Post: buran
  python multiple try except block in my code -- can we shorten code mg24 10 5,892 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  repeating a user_input astral_travel 17 2,136 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  if else repeating Frankduc 12 2,425 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  matching a repeating string Skaperen 2 1,197 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  "If Len(Word) == 0" Code Block in Pig Latin Program new_coder_231013 3 2,017 Jan-02-2022, 06:03 PM
Last Post: deanhystad
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 2,302 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  Random Number Repeating Tzenesh 5 3,925 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  Repeating lines of code by an input Josh_Albanos 3 2,352 Oct-15-2020, 01:04 AM
Last Post: deanhystad
  factorial, repeating Aldiyar 4 2,738 Sep-01-2020, 05:22 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020