Start Your Journey with Linux Command Line
If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in LAB: Comparison operators and conditional execution
Spathiphyllum, more commonly known as a peace lily or white sail plant, is one of the most popular indoor houseplants that filters out harmful toxins from the air. Some of the toxins that it neutralizes include benzene, formaldehyde, and ammonia.
Imagine that your computer program loves these plants. Whenever it receives an input in the form of the word Spathiphyllum, it involuntarily shouts to the console the following string: "Spathiphyllum is the best plant ever!"
Write a program that utilizes the concept of conditional execution, takes a string as input, and:
prints the sentence "Yes - Spathiphyllum is the best plant ever!" to the screen if the inputted string is "Spathiphyllum" (upper-case)
prints "No, I want a big Spathiphyllum!" if the inputted string is "spathiphyllum" (lower-case)
prints "Spathiphyllum! Not [input]!" otherwise. Note: [input] is the string taken as input.
Test your code using the data we've provided for you. And get yourself a Spathiphyllum, too!
Test Data
Sample input: spathiphyllum
Expected output: No, I want a big Spathiphyllum!
Sample input: pelargonium
Expected output: Spathiphyllum! Not pelargonium!
Sample input: Spathiphyllum
Expected output: Yes - Spathiphyllum is the best plant ever!
This lab uses the peace lily as a playful subject for practicing conditional execution and string comparison. The program takes a single string as input and must react differently depending on exactly what was typed. The three cases demonstrate how an if, an elif, and an else work together to route the program along one of several paths based on the value of the input.
The first branch compares the input against "Spathiphyllum" with the exact capitalization. Because Python string comparisons are case-sensitive, the string "spathiphyllum" in lowercase is a completely different value from the one with a capital S. This distinction is the heart of the exercise: the user's spelling, including letter case, determines which message the program prints.
The code stores the plant name in a variable and then checks it. If the input equals "Spathiphyllum", the program prints "Yes - Spathiphyllum is the best plant ever!". An elif then catches the all-lowercase version "spathiphyllum" and prints "No, I want a big Spathiphyllum!". Finally, the else branch handles every other word, printing "Spathiphyllum! Not " followed by the input and an exclamation mark, as in "Spathiphyllum! Not pelargonium!".
Because elif is checked only when the previous condition failed, the branches are mutually exclusive and exactly one message is printed. The final else guarantees the program always produces output, even for an unrecognized plant. This simple branching structure is the foundation of much more complex decision-making in Python, and the case sensitivity it demonstrates is a common source of bugs when comparing user text.
if/elif/else chain runs exactly one branch for any given input.elif is evaluated only when the if condition was false.else catches all remaining inputs and always produces some output.Python compares strings character by character, and uppercase and lowercase letters are treated as different characters. So "Spathiphyllum" does not equal "spathiphyllum", which is exactly the behavior the lab relies on.
The if checks the first condition, and each elif is checked only if every condition above it was false. This makes the tests run in order and stops at the first match.
Control falls through to the else branch, which prints "Spathiphyllum! Not " followed by the actual input and an exclamation point.
Yes. The == operator works on any two strings and returns True only when every character, including letter case, matches exactly.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.