2

In my code I have two weights that need to equal 1.0. If not I need a print statement saying the conditions are not met and it wont continue to run the rest of the code.

My issue is that if conditions are met, it runs the code forever and it is supposed to continue with the rest of the code. And if they are not met I just want to make sure that it will not run the rest of my code and stop altogether. Two issues that need to be addressed I guess.

No numpy allowed pls.

weight_1 = 0.5
weight_2 = 0.5

while True:
    if ((weight_1 + weight_2) == 1.0):
        print ("Weights are appropriate")
    else:
        print("Error! Weights limits not appropriate!")
        break;
1
  • Put a return statement after the print. But your code is not very elegant to start with. Commented Feb 13, 2017 at 14:05

1 Answer 1

3
import sys 
 weight_1 = 0.5
 weight_2 = 0.2

if ((weight_1 + weight_2) == 1.0):
        print ("Weights are appropriate")

else:
        print("Error! Weights limits not appropriate!")
        sys.exit()
Sign up to request clarification or add additional context in comments.

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.