Conditional Statements and Logic
raw_input() captures text from the keyboard and returns it as a string to your program.
From Keyboard to Variable
An input-driven program begins outside the program: a user types at the keyboard. The raw_input() function connects that action to your code. It displays a prompt, waits for the user to type and press Enter, captures the typed text, and returns that text as a string. When the returned value is assigned to a variable, the program can process the response and use it in a decision.
The assignment happens after raw_input() returns. First, the prompt is displayed. Next, the program waits. The user types and presses Enter. The text is captured and returned, and then it is stored in something. At that point, something contains the user's response as a string.
A Decision from Text
A conditional decision uses processed input to choose a result. In the palindrome example, the program compares the original text with a reversed version. If the two strings match, the text is identified as a palindrome. If they do not match, it is not identified as a palindrome. The important flow is that the comparison uses the string returned by raw_input(), rather than an abstract value disconnected from the user's response.
Tracing a palindrome response
Follow the response through a palindrome checker when the user enters a word that reads the same forwards and backwards.
Prompt: The program displays a request for text and waits for the user to type and press Enter.
Capture: raw_input() returns the typed text as a string, and the main program stores it in a variable.
Process: The response is passed to palindrome-checking logic. The text is reversed and compared with the original.
Decision: Because the original and reversed strings match, the program selects the palindrome result.
Output: The program displays the result of the comparison.
The response travels through input capture, palindrome checking, and output. The comparison determines which result is displayed.
The Complete Palindrome Program
The source example organizes the task into two helper functions and a main input-and-output stage. reverse() flips a string with the slice notation [::-1]. is_palindrome() compares the original text with its reversed version. The main program calls raw_input(), sends the captured string to is_palindrome(), and prints the result.
def reverse(text): return text[::-1] def is_palindrome(text): return text == reverse(text) text = raw_input("Enter text: ") if is_palindrome(text): print "The text is a palindrome." else: print "The text is not a palindrome."
Tracing Unexpected Results
When the output is unexpected, do not inspect only the final message. Trace the program step by step and inspect the state of the data at each stage. For an input-driven palindrome checker, the main stages are input capture, palindrome checking, and output. The divergence from expectation occurred in one of those stages.
- First verify what text the user actually entered and confirm that raw_input() captured it.
- Next verify the value passed into the palindrome-checking function.
- Then verify the reversed text and the comparison result.
- Finally verify that the output stage displays the result produced by the comparison.
Mistakes Beginners Make
Treating the value returned by raw_input() as a number automatically.
raw_input() returns text as a string.
Fix:
Convert the string to int or float when numeric operations are required.Skipping the input-capture stage during debugging.
The unexpected result may have begun when input was captured, during processing, or during output.
Fix:
Trace the response through input capture, palindrome checking, and output.Assuming the conditional result without checking the comparison.
The decision depends on whether the two strings match.
Fix:
Inspect the original text, its reversed form, and the comparison result.Ignoring whitespace or case sensitivity when handling text.
The source specifically identifies whitespace and case sensitivity as details to handle carefully when working with captured text.
Fix:
Account for whitespace and case sensitivity when testing and processing real user input.
Practice the Flow
Write down the value at each stage of an input-driven palindrome checker: the text returned by raw_input(), the reversed text, the comparison result, and the displayed result. Then test the program with one response that reads the same forwards and backwards and one response that does not.
Hints
- Begin with the fact that raw_input() returns a string.
- Use [::-1] to identify the reversed text.
- The conditional result depends on whether the original and reversed strings match.
Key Takeaways
- raw_input() displays a prompt, waits for keyboard input, and returns the typed text as a string.
- The returned string is stored in a variable so that program logic can process it.
- A palindrome checker reverses the captured text and compares the reversed version with the original.
- Conditional output depends on whether those two strings match.
- When output is unexpected, trace input capture, processing, and output in order.
Key Takeaways
- raw_input() moves text from the keyboard into a program variable as a string.
- Conditional logic can use the captured response to select between results.
- The palindrome example reverses a string and compares it with the original.
- Debugging is a step-by-step trace through input capture, processing, and output.
- Numeric responses require conversion to int or float before numeric operations.