top of page

Python If Conditions

  • Mar 6, 2022
  • 2 min read

Updated: Apr 27, 2025

The case below is a typical “IF conditions”:


Write an input function that takes the traffic light as the input.

When the light is green, the function should return 'move'

When the light is orange, the function should return 'caution'

When the light is red, the function should return 'stop'

Otherwise, the function should return 'invalid signal'


Let's use Python to solve this problem.


First of all, we should know that “green”, ”orange” and ”red” are inputs, “move”, “caution” and “stop” are outputs. So when we write the code, “orange” is the condition, “caution” is the result that the system would give us. The code is shown as below:




There is no content in “()” of “s = input ()” since the content in the input represents what is the default value if there is no following inputs. For example, if I write “s = input (“green”)”, it means if I do not enter a value, then “green ” would be the default value.


As you can see, I also used “elif” in line 4 and line 6, “elif” is short for “else if”, it means if the previous if condition does not satisfy with the value that users input, then it will turn to elif condition to see if the input value matches it or not. You may notice that there is an “else”, we do not need to write any conditions behind it as it represents what result we could get if all of the previous conditions do not satisfy with input values.


Now we can run this code to see if we succeed or not:


From this screenshot, we can see that if I enter ”orange”, the system shows “caution”, so we can end this process.


FUNTERN Associate: Jin Wang

Comments


bottom of page