def wanted_to_execute_two_ifs(x): if x > 5: print("X is greater than 5") if x > 10: print("X is greater than 10") def didnt_want_to_execute_two_ifs(x): if x > 5: print("X is greater than 5 but not greater than 10") if x > 10: print("X is greater than 10") def caught_by_wrong_condition_greater_than(x): if x > 5: print("X is only greater than 5 but not greater than 10") elif x > 10: print("X is greater than 10") def caught_by_correct_condition_greater_than(x): if x > 5: print("X is only greater than 5 but not greater than 10") elif x > 10: print("X is greater than 10") def caught_by_wrong_condition_less_than(x): if x < 10: print("X is less than 10 but not less than 5") elif x < 5: print("X is less than 5") def caught_by_correct_condition_less_than(x): if x < 10: print("X is less than 10 but not less than 5") elif x < 5: print("X is less than 5") print("What if we want to do both things, if X is greater than 5 AND greater than 10, and so used two ifs on purpose?") wanted_to_execute_two_ifs(15) print("") print("What if we want to do only one thing: something if X is greater than 5 or somethign else if greater than 10, but we mistakenly use if instead of elif?") didnt_want_to_execute_two_ifs(15) print("") print("What if we have our conditions out of order when using inequalities in our if/elif?") caught_by_wrong_condition_greater_than(15) print("") print("Another example, using the wrong order in a less-than comparision") caught_by_wrong_condition_less_than(2) print("") print("What if we have our conditions in the correct order when using inequalities in our if/elif?") caught_by_correct_condition_greater_than(15) print("") print("Another example, using the correct order in a less-than comparision") caught_by_correct_condition_less_than(2)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question