Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python break from if statement to else

(I'm a Python newbie, so apologies for this basic question, I for some reason couldn't find an answer to.)

I have a nested if statement with the if statement of an if/else block. In the nested if statement, if it it meets the criteria, I'd like the code to break to the else statement. When I put a break in the nested if, though, I'm not sure if it's breaking to the else statement.

I'd like to find the longest substring in alphabetical order of a given string, s. Here's my code:

s = 'lugabcdeczsswabcdefghij'
longest = 1
alpha_count = 1
longest_temp = 1
longest_end = 1
for i in range(len(s)-1):
    if (s[i] <= s[i+1]):
        alpha_count += 1
        if (i+1 == (len(s)-1)):
            break
    else:
        longest_check = alpha_count
        if longest_check > longest:
            longest = longest_check
            longest_end = i+1
        alpha_count = 1
print(longest)
print('Longest substring in alphabetical order is: ' + 
    s[(longest_end-longest):longest_end])

(Yes, I realize there's surely lots of unnecessary code here. Still learning!)

At this nested if:

if (i+1 == (len(s)-1)):
            break

...if True, I'd like the code to break to the else statement. It doesn't seem to break to that section, though. Any help?

like image 864
Jim Avatar asked Sep 16 '16 16:09

Jim


People also ask

How do you do a break statement in if else in Python?

In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered. You'll put the break statement within the block of code under your loop statement, usually after a conditional if statement. In this small program, the variable number is initialized at 0.

How do you break out of if else statements?

The break statement ends the loop immediately when it is encountered. Its syntax is: break; The break statement is almost always used with if...else statement inside the loop.

What is exit () in Python?

_exit() method in Python is used to exit the process with specified status without calling cleanup handlers, flushing stdio buffers, etc. Note: This method is normally used in the child process after os. fork() system call. The standard way to exit the process is sys. exit(n) method.

Can you use break to break out of an if statement?

breaks don't break if statements. A developer working on the C code used in the exchanges tried to use a break to break out of an if statement. But break s don't break out of if s. Instead, the program skipped an entire section of code and introduced a bug that interrupted 70 million phone calls over nine hours.


1 Answers

break is used when you want to break out of loops not if statments. You can have another if statement that executes this logic for you like this:

if (s[i] <= s[i+1]):
    alpha_count += 1
elif (i+1 == (len(s)-1)) or (add boolean expression for else part in here too something like s[i] > s[i+1]):
    longest_check = alpha_count
    if longest_check > longest:
        longest = longest_check
        longest_end = i+1
    alpha_count = 1

What this snippet is doing is evaluating two booleans, both are for the else part. However, it says either execute in case of else from first if or in case of (i+1 == (len(s)-1))

like image 192
Kafo Avatar answered Sep 28 '22 08:09

Kafo