Python crash course for beginner 1 St Step : Conditional statement with “if, elif and else chain”
Basic syntax and project base introduction
A conditional statement in python can be evaluated as True or False and is also known as a conditional test. In python uses the values True and
False to decide whether the code in an if statement should be executed. If a
conditional test evaluates to True, Python executes the code following the if
statement. If the test evaluates to False, Python ignores the code following
the if statement.
if-elif-else Chain
In the real world often, you need to test more than two possible situations, and to evaluate. For this you can use Python’s if-elif-else syntax. Keep in mind that the python programming language only executes one block in the if-elif-else chain. This runs each conditional test sequentially until one pass. When a test passes, the code following that test is executed and python skips the rest of the tests. For example
if conditional_one:
do something
elif conditional_two:
do something
elif conditional_three:
do something
else:
do someting
You can do any conditional test in the first line and almost anything action in indented block after test. If the test is conditional is True, Python will execute the code following the if statement. If the test evaluates to False, Python ignores the code following the if statement. Copy and run code sample
Output
#choose the first menuSelect menu : Pecel lele
Enter quantity: 12
Price : Pecel lele = 16000
Quantity = 16000 x 12 = 192000#choose the second menuSelect menu : Ayam goreng
Enter quantity: 12
Price :Ayam goreng = 20000
Quantity = 20000 x 12 = 240000#choose the third menu
Select menu : Ikan goreng
Enter quantity: 10
Price :Ikan goreng = 22000
Quantity = 22000 x 10 = 220000#the result if you don't choose the menu that matches the list
Select menu : Gado gado
Enter quantity: 1
Food not available, :)
So, how is it? easy yes. “Please explore again until you understand.”
Thanks, keep coding and change your world :)