rric

loop_1

Nov 13th, 2023 (edited)
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None | 0 0
  1. # Demonstrate various methods to create a list of numbers in Python.
  2. # Copyright 2023 Roland Richter                           [Mu: Python 3]
  3.  
  4. # If you have to create a list which holds numbers 0, 40, 80, ..., 400,
  5. # there are several methods to do it:.
  6. # 1. You could simple write a list with all items in it:
  7. nums_a = [0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400]
  8.  
  9. # ... really tedious typing, isn't it? There are better ways.
  10. # 2. The second way is still a bit tedious:
  11. nums_b = []             # Create an empty list, then ...
  12. num = 0
  13. while num <= 400:       # ... (until 400 is reached)
  14.     nums_b.append(num)  # ... start appending numbers: first 0, ...
  15.     num += 40           # ... then 40, 80, and so on.
  16.  
  17. # Now, open a handbook such as
  18. # https://www.w3schools.com/python/python_strings.asp
  19. # and start to FIX my code; but ONLY code inside ↓ / ↑ lines!
  20.  
  21. # 3. Python provides another way to loop through numbers:
  22. # the "for" loop with the range(start, stop, step) function, where
  23. # start is the integer to start the iteration with,
  24. # stop is the integer to stop with (not included),
  25. # step is the integer specifying the increment.
  26. # A common source of errors is the fact that stop is NOT included.
  27. # FIX the numbers in "range" to create all numbers 0, 40, ... 400:
  28. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  29. nums_c = []
  30. for num in range(0, 400, 40):
  31.     nums_c.append(num)
  32. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  33.  
  34. # 4. Two of the arguments of range(start, stop, step) are optional:
  35. # start is the integer to start with -- optional (default: 0),
  36. # stop is the integer to stop with (not included) -- required,
  37. # step is specifying the increment -- optional (default: 1).
  38. # So, writing "for num in range(10)" means "for num in range(0, 10, 1)",
  39. # which is another way of saying "loop over 10 numbers, from 0 to 9".
  40. # FIX the numbers in "range" to create all numbers 0, 40, ... 400:
  41. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  42. nums_d = []
  43. for idx in range(10):
  44.     nums_d.append(40 * idx)
  45. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  46.  
  47. # 5. Python provides an even shorter syntax, called list comprehension.
  48. # FIX the (hopefully obvious) computation error here.
  49. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  50. nums_e = [42 * idx for idx in range(11)]
  51. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  52.  
  53. # 6. Your turn!
  54. # Create [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165]
  55. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  56. nums_f = []
  57. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  58.  
  59. # 🖐 HALT! No code editing below this line!
  60. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  61.  
  62. nums40 = [0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400]
  63. nums15 = [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165]
  64. assert nums_a == nums_b, "\n  nums_b is " + str(nums_b) + "\n  expected: " + str(nums40)
  65. assert nums_a == nums_c, "\n  nums_c is " + str(nums_c) + "\n  expected: " + str(nums40)
  66. assert nums_a == nums_d, "\n  nums_d is " + str(nums_d) + "\n  expected: " + str(nums40)
  67. assert nums_a == nums_e, "\n  nums_e is " + str(nums_e) + "\n  expected: " + str(nums40)
  68. assert nums_a == nums_f, "\n  nums_f is " + str(nums_f) + "\n  expected: " + str(nums15)
  69.  
  70. print("+---------+---------+---------+---------+---------+---------+")
  71. input("FIXED You did it!")
  72.  
  73. # ----------------------------------------------------------------------
  74. # This program is free software: you can redistribute it and/or modify
  75. # it under the terms of the GNU General Public License as published by
  76. # the Free Software Foundation, either version 3 of the License, or
  77. # (at your option) any later version.
  78. #
  79. # This program is distributed in the hope that it will be useful,
  80. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  81. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  82. # GNU General Public License for more details.
  83. #
  84. # You should have received a copy of the GNU General Public License
  85. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  86.  
Advertisement
Add Comment
Please, Sign In to add comment