| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + #!/usr/bin/env python3 | ||
| 2 | + """ | ||
| 3 | + Author : kyclark | ||
| 4 | + Date : 2019-05-17 | ||
| 5 | + Purpose: Looping N times | ||
| 6 | + """ | ||
| 7 | + | ||
| 8 | + import os | ||
| 9 | + import sys | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + # -------------------------------------------------- | ||
| 13 | + def main(): | ||
| 14 | + """main""" | ||
| 15 | + args = sys.argv[1:] | ||
| 16 | + | ||
| 17 | + if len(args) != 1: | ||
| 18 | + print('Usage: {} NUM'.format(os.path.basename(sys.argv[0]))) | ||
| 19 | + sys.exit(1) | ||
| 20 | + | ||
| 21 | + arg = args[0] | ||
| 22 | + | ||
| 23 | + if arg.isdigit(): | ||
| 24 | + for i in range(1, int(arg) + 1): | ||
| 25 | + print('{} time{}'.format(i, '' if i == 1 else 's')) | ||
| 26 | + else: | ||
| 27 | + print('"{}" is not a number'.format(arg)) | ||
| 28 | + | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + # -------------------------------------------------- | ||
| 32 | + if __name__ == '__main__': | ||
| 33 | + main() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -266,6 +266,51 @@ codons.py | |||
| 266 | 266 | default_arg.py | |
| 267 | 267 | ```` | |
| 268 | 268 | ||
| 269 | + ## Loop N Times | ||
| 270 | + | ||
| 271 | + If you want to go through a loop some defined number of times, maybe combine `for` and `range`: | ||
| 272 | + | ||
| 273 | + ```` | ||
| 274 | + $ cat -n looping_n_times.py | ||
| 275 | + 1 #!/usr/bin/env python3 | ||
| 276 | + 2 """ | ||
| 277 | + 3 Author : kyclark | ||
| 278 | + 4 Date : 2019-05-17 | ||
| 279 | + 5 Purpose: Looping N times | ||
| 280 | + 6 """ | ||
| 281 | + 7 | ||
| 282 | + 8 import os | ||
| 283 | + 9 import sys | ||
| 284 | + 10 | ||
| 285 | + 11 | ||
| 286 | + 12 # -------------------------------------------------- | ||
| 287 | + 13 def main(): | ||
| 288 | + 14 """main""" | ||
| 289 | + 15 args = sys.argv[1:] | ||
| 290 | + 16 | ||
| 291 | + 17 if len(args) != 1: | ||
| 292 | + 18 print('Usage: {} NUM'.format(os.path.basename(sys.argv[0]))) | ||
| 293 | + 19 sys.exit(1) | ||
| 294 | + 20 | ||
| 295 | + 21 arg = args[0] | ||
| 296 | + 22 | ||
| 297 | + 23 if arg.isdigit(): | ||
| 298 | + 24 for i in range(1, int(arg) + 1): | ||
| 299 | + 25 print('{} time{}'.format(i, '' if i == 1 else 's')) | ||
| 300 | + 26 else: | ||
| 301 | + 27 print('"{}" is not a number'.format(arg)) | ||
| 302 | + 28 | ||
| 303 | + 29 | ||
| 304 | + 30 | ||
| 305 | + 31 # -------------------------------------------------- | ||
| 306 | + 32 if __name__ == '__main__': | ||
| 307 | + 33 main() | ||
| 308 | + $ ./looping_n_times.py 3 | ||
| 309 | + 1 time | ||
| 310 | + 2 times | ||
| 311 | + 3 times | ||
| 312 | + ```` | ||
| 313 | + | ||
| 269 | 314 | ## Skip an iteration of a loop | |
| 270 | 315 | ||
| 271 | 316 | Sometimes in a loop (`for` or `while`) you want to skip immediately to the top of the loop. You can use `continue` to do this. In this example, we skip the even-numbered lines by using the modulus `%` operator to find those line numbers which have a remainder of 0 after dividing by 2. We can use the `enumerate` function to provide both the array index and value of any list. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments