I'm new to Python. Actually I implemented something using Java as shown below.
for(;;){
switch(expression){
case c1: statements
case c2: statements
default: statement
}
}
How do I implement this in Python?
Use while loop:
while True:
if condition1:
statements
elif condition2:
statements
...
else:
statements
while True:
# do stuff forever
Formally, there's no switch
statement in Python; it's a series of nested if-elif-else
statements.
Infinite loops are accomplished by the while True
statement.
All together:
while True:
if condition_1:
condition_1_function
elif condition_2:
condition_2_function
elif condition_3:
condition_3_function
else: # Always executes like "default"
condition_default_function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With