eraserhd
eraserhd

Reputation: 1

how can i return something and break the loop same time in python

while True:
    if abs(func_value) < epsilon:
        return (x_0, itNum)
    if abs(func_value) < epsilon:
        break

    else:
        assert deriv_value != 0
        x_0 = x_0 - (func_value / deriv_value)
        itNum += 1

I do not want to write twice the condition. Is there another way to write this more appropriate?

i am so sorry guys,,

actually it continues with else statement that updates the func_value . i have just fixed

Upvotes: 0

Views: 3474

Answers (1)

TheSoundDefense
TheSoundDefense

Reputation: 6935

The return command immediately exits whatever function you're in, so if you're returning, there is no need to break out of a while loop. You've already broken out of it, along with that entire function.

Upvotes: 7

Related Questions