Skip to content

Latest commit

 

History

History
117 lines (95 loc) · 1.66 KB

loop.mdx

File metadata and controls

117 lines (95 loc) · 1.66 KB
title
Loop

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

for loop

<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: '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. <Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: '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