Skip to main content

Loop

for loop

for i in range(10):
print(i)

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.

current_num = 0
while current_num < 10:
current_num += 1
if current_num % 2 == 0:
continue
print(current_num) # print odd numbers only