mikeizbicki / cmc-csci040

Computing for the Web
37 stars 58 forks source link

'' condition in an if else statement #250

Closed fjennison22 closed 1 year ago

fjennison22 commented 1 year ago

In question 7 of Quiz02 it asks for the output of the following code:

if '': result = 0 else: result = 1 print('result=', result)

the result is 1 but I'm not sure what the if '' condition means/is doing

henrylong612 commented 1 year ago

Hey Freya! Mike can correct me if I'm wrong, but I'm pretty sure that the '' value signifies an empty string, which is technically false in Boolean terms. Because '' is false, it returns the value under "else:" which is 1. If you added the word 'dog' inside of the quotation marks, you would get a true Boolean value, and it would return 0. Mike, please correct me if I'm wrong.

mikeizbicki commented 1 year ago

@henrylong612's explanation is essentially correct, but I'll add the technical caveat that only False is "false" in python. Other values like 0 and '' are "falsey".