Beginner, no installation required, Python tutorial on how to use if, elif, else statements on MecSimCalc
This tutorial outlines how to use if, elif, else statements online using MecSimCalc. This is done through an example application which displays various messages depending on a number input which is provided by the user.
Step 1: Info
- Since this is a tutorial, the information section has been filled out as such
- The images are the defaults, which can be changed as the creator desires
Step 2: Inputs
- Start with an input group using the “+” button at the bottom right and scrolling down to actions
- Set the name of this group to “Number Input”
- Add a number input using the same “+” button, and set the label to “Number Input”, and the variable name to num_1
- Ensure that the input section fits a mobile user’s screen as well by clicking the cellphone icon and adjusting the input sizes
Step 3: Code
An if, then, else statement is a useful and simple way to have a process happen based on one or several conditions that are set. This process can be outputting a certain message, or proceeding to a completely different set of if, elif, else statements based on the previous condition in the initial if, elif, else statement. For example, in a calorie calculating app, the set of rules for calculating total calories may be different for males and females, thus an if, elif, else statement may be used in conjunction with another one.
def main(inputs):#Statementif 0 <= inputs['num_1'] <= 50:state = 1elif 50 < inputs['num_1']:state = 2else:state = 3return {"state": state}
- Start with defining the inputs using def main(inputs)
- An if, elif, else statement starts with an “if” and is followed by a condition; in this case, if the input number (which is copied via the input variables section) is greater or equal to 0, but less than or equal to 50, then state = 1. A “:” after the ending of the if statement is required
- “State” is a variable which a value is assigned to, this is for the purpose of later use in the output section, where a message can be displayed
- “elif” which is short for “elseif”, is used next for another condition; specifically, if the input number is greater than 50, state = 2
- More conditions could be added using “elif”, however in this case the only option for the number is to be less than 0, since the conditions of greater than 50 and being between 0 and 50 are already set.
- Thus, “else” is used, and no condition needs to be written since it is the only other option. Remember the “:” at the end
- Lastly, “return” is used to display the output variable “state”
Step 4: Output
In order to display the messages through the application, another if, elif, else statement must be used in the output section, this time using the “state” output variable.
- The formatting is up to the developer, however the if, elif, else statement is used similarly to the third step
- using the “if” function next to the “for” function, an automatic template is set in the output section
- Next, state conditions for the output variable “state”. In this case, if “state” = 1, the message “The number is between 0 and 50” is displayed and etc. for the values 2 and 3 of “state”.
- The statement is ended with {% endif %}
Step 5: Docs
Documentation is not required for a tutorial, however usually a summary of how the application works is good to note.
Step 6: Preview
Ensure the application works with all three conditions, and the application is done after pressing publish!