[Solved] If statement vs if else


Based on your if test, one of the following two things happens:

  • you set predict[passenger_numberId] to 1 first, then immediately, set it to 0.
  • you set predict[passenger_numberId] to 0.

So, without an else statement, you always set predict[passenger_numberId] to 0, and it doesn’t actually matter what the outcome of the passenger.gender == 'female' test is.

By using else you only set predict[passenger_numberId] to 0 when the if test didn’t pass, and to 1 when it does pass.

In other words, by using else your code now takes one of two paths, and the outcome differs based on the choice of paths. Without the else, the value is always set to 0, regardless of the test.

0

solved If statement vs if else