function - Break out of Python for loop -


i wondering if there's way can have loop print out statement right after 5th loop instead of calling 6 times , stopping @ 5 using if statement.

is there more efficient way accomplish this? thanks.

for counter in range (6):   if counter == 5:     print("maximum speed reached!\n")     break ##break out of loop if condition met.   mycar.accelerate()   time.sleep(1) ##add 1 second delay each loop. 

you iterate until 5 instead of 6 , print message out of loop:

for counter in range(5):     # remove if statement:     # if counter == 5:     #     print("")     #     break     mycar.accelerate()     time.sleep(1)  print("maximum speed reached!") 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -