Week 2 - Section 1 - Controlling the flow v2
Chapter 3.5 - The For loop
3.5 - The For loop
When i use a loop? When i want to repeat a block of code. The for loop is used when i know how many times i wan the code to be executed. Copy the following code to Thonny's editor. To view the execution step by step you can press the Debug button, after that the Step into (F7) and finally Step over(F6). Take a closer look at the variable x in the right pane of Thonny.
for i in range(10):
print("I will not chew gum in class.")
Also try the following
for i in range(5):
print("Please,")
print("Can I go to the mall?")
And changed to this
for i in range(5):
print("Please,")
print("Can I go to the mall?")
Try the following code and try to explain what is its purpose :
for x in range(10):
print (i, "* 5 =", i*5)
Do you have an idea of how we can make it better? Try this one now :
for i in range(1,11):
print (i, "* 5 =", i*5)
And now this one :
number = int(input("Please write a number for 1 to 10 : "))
for i in range(1,11):
print (i, "* ",number," =", i*number)