Loop
for loop
- Input
- Output
for i in range(10):
print(i)
0
1
2
3
4
5
6
7
8
9
while loop
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ""
while message != 'quit':
message = input(prompt)
print(message)
- Use a
flag
to do the same:
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
break
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
while True:
message = input(prompt)
if message == 'quit':
break
else:
print(message)
continue
continue
takes to the beginging of loop.
- Input
- Output
current_num = 0
while current_num < 10:
current_num += 1
if current_num % 2 == 0:
continue
print(current_num) # print odd numbers only
1
3
5
7
9