Photo by Lauren Mancke on Unsplash
Lost in the Wonderland of Python: A Humorous Exploration of Nested If-Else Statements and User Inputs!
Welcome to the whimsical wonderland of Python programming, where nested if-else statements and user inputs come alive! Prepare to embark on a hilariously delightful journey filled with coding conundrums and interactive surprises. As we navigate through the twists and turns of nested conditionals, we'll uncover the secrets of decision-making and create a laugh-out-loud Python program together. So, fasten your seatbelts and get ready for a humorous escapade into the magical realm of Python conditionals – where logic meets laughter!
In this demonstration, we'll explore how to combine input functions with nested if-else statements using two straightforward examples. You'll see that it's surprisingly easy and fun to work with!
Example 1
In this tutorial, we will revisit the example showcased in the blog post titled 'The Elsceptional Odyssey: A Python's Adventure into the Depths of Nested If-Else.' You can find the link to the blog post here: [https://jmn950-dev.hashnode.dev/the-elsceptional-odyssey-a-pythons-adventure-into-the-depths-of-nested-if-else].
Step 1
Create the below variable
Step 2
Proceed to create the following 'if' function based on the specified requirements.
Step 3
Execute the program by entering a numerical value, for example, 25. As a result, you will observe the following output.
Example 2
Consider the scenario where you, as a programmer, are tasked with developing software for a climatologist responsible for daily weather recordings. In this context, we will explore the application of nested if-else statements and input handling to fulfill this requirement and in this case, it will be temperature recordings.
Step 1
Step 2
Create the following 'if' function based on the specified requirements.
Let's break the code into simple explanations.
The teacher tells the program that he/she has a student's score, and the score is 100.
The program looks at the score and says, "Oh, the score is less than 200!"
Since the score is less than 200, the machine starts looking closely at the score to find out the exact grade.
It checks if the score is 150. But it's not! So, the machine moves on to the next check.
The program then checks if the score is 100. Amen! The score is 100, and the program says, "The student's score is 100, which means the student got Grade B!"
The program checks all the possibilities! It knows that 100 is not equal to 150 or 50.
Now, it checks if the score is 50. It's not! So, the program knows the student didn't get a Grade C.
Since the score is not 50, the program looks if the score is less than 50. But 100 is greater than 50! So, the program says, "The score is more than 50, and it's 100! So, the student got a Grade B"!
The program finishes its job and tells us the final result, "The student's score is 100, which means the student got Grade B!"
Step 3
Run the program and you should get the below code.
Note: It is essential to acknowledge that the program can be executed without resorting to the utilization of nested-if else statements. It is advisable to consider nested-if else statements as a last option when crafting code solutions.
With that being said, we conclude our discussion for today. In the subsequent article, we will delve into the topic of functions.