Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+3 votes

If I type in

if (some condition):
     
elif (some other condition):
      (this is the code I want manipulated) 

I wanted to use the first if statement to make the code skip to the end, but if I leave it blank it gives me an error.

I ended up putting in a meaningless variable to get rid of the error and the code worked, but I was wondering if there was a command that can tell python "condition met, go to the end of the loop now plz"?

asked in CSC201 Spring 2021 by (8 points)

2 Answers

+3 votes

I think the best way to accomplish this would be to invert your condition with not.
Instead of

if (some condition):
   
elif (other condition):
    code

Try

if not (some condition) and (other condition):
answered by (8 points)
+3 votes

I'm not sure if we learn this concept in class, but you can use continue and break to skip to the rest of the loop.
For example, with continue, it will skip the rest of the code inside a loop for the current iteration to the next iteration:

number = 0
while number  < 10:
    if number == 5:
        continue
    print(number)

Output:

1
2
3
4
6
7
8
9
10

With break, it will immediately stop the the loop:

number = 0
while number < 10:
    number = number + 1
    if number == 5:
        break
    print(number)

Output:

1
2
3
4
answered by (2.2k points)
...