Defining and Calling Functions
The raw_input() function is the gateway for this process.
From Keyboard to Variable
A user response does not appear in a program all at once. It travels through a sequence: the program displays a prompt, waits for input, receives the text after the user presses Enter, and stores the returned string in a variable. The raw_input() function is the gateway for this process.
raw_input() captures text from the keyboard and returns it as a string to your program.
Storing a Response
Follow what happens when a program uses something = raw_input("Enter text: ").
- 1Display
The prompt string Enter text: is displayed.
- 2Wait
The program waits for the user to type and press Enter.
- 3Capture
The typed text is captured and returned by
raw_input(). - 4Store
The returned text is stored in the variable something as a string.
The variable something now contains the user's typed text, ready for processing.
Following the Response
After raw_input() returns text, the program can process the value and produce an output. The complete path has three important stages: input capture, data processing, and output. Keeping these stages separate makes it easier to determine where an unexpected result began.
Definitions and Calls
A function definition describes a reusable piece of program logic. A function call uses that function so its logic can be applied to a value. In the source example, reverse() is defined to flip a string backwards, and is_palindrome() is defined to compare original text with its reversed version. The main program calls is_palindrome() after it captures the user's text.
def reverse(text):
return text[::-1]
def is_palindrome(text):
return text == reverse(text)
something = raw_input("Enter text: ")
result = is_palindrome(something)
print result- 1The program defines
reverse()before using it. - 2The program defines
is_palindrome(), which usesreverse()as part of its comparison. - 3The program calls
raw_input()and stores the returned string in something. - 4The program calls
is_palindrome(something), passing the stored text into the checking logic. - 5The result is printed after the input has been processed.
- The
reverse()definition describes logic for flipping a string backwards. - The
is_palindrome()definition compares the original text with the result fromreverse(). - The
raw_input()call captures the user's text and stores it in something. - The
is_palindrome()call applies the checking logic to something. - The final statement prints the result.
Palindrome Checker
A palindrome is a word or phrase that reads the same forwards and backwards. The complete program uses two helper functions: reverse() flips the text backwards, and is_palindrome() compares the original text with the reversed text. The main program captures input, passes it to the checking function, and prints the result.
Tracing the Palindrome Program
Follow the response through the program when the user enters a palindrome.
- 1Input capture
raw_input()displays the prompt, waits, and returns the user's text as a string. - 2Function call
The stored string is passed to
is_palindrome(). - 3Reverse
is_palindrome()usesreverse()to create the backwards version of the text. - 4Comparison
The original string is compared with its reversed version.
- 5Output
The comparison result is printed.
The program's output depends on whether the original text and reversed text match.
This example illustrates why definitions and calls must be traced separately. The helper functions describe the processing steps, but the processing is used only when the main program calls the appropriate function with the captured input.
Finding the Divergence
When the output is unexpected, do not inspect only the final line. Trace the state of the data step by step. Ask whether the prompt and capture happened as expected, whether the stored string is what you intended, whether the processing logic handled that string correctly, and whether the output reflects the processing result.
Common mistake Treating the value returned by raw_input() as a number automatically.
What it looks likeA value captured with raw_input() is used directly for numeric operations.
Why it failsraw_input()returns a string.Do this insteadConvert the returned value to int or float when numeric operations are needed.
Common mistake Assuming the captured text is exactly the text the program expected.
What it looks likeA palindrome check produces an unexpected result even though the visible word appears correct.
Why it failsWhitespace and case sensitivity can affect how text is processed.
Do this insteadTrace and inspect the input-processing path, accounting for whitespace and case.
Common mistake Debugging only the final output.
What it looks likeThe result is wrong, but the input capture and processing stages are not checked.
Why it failsThe divergence may have occurred during input capture, processing, or output.
Do this insteadTrace execution step by step and examine the data state at each stage.
Use the three-stage model when debugging: input capture, palindrome checking or other processing, and output. Identify the value at each stage before deciding which part of the program needs attention.
Trace It Yourself
Trace a palindrome-checking program from the moment its prompt appears until its result is printed. For each stage, write down what happens to the user's text: capture, storage, processing, and output. Then explain what you would inspect first if the result did not match your expectation.
Hints
- Begin with the fact that
raw_input()returns a string. - Separate the stored input from the work performed by the helper functions.
- Check whether the divergence occurred during input capture, processing, or output.
- What prompt does the program display?
- What string does
raw_input()return? - Which variable stores that string?
- Which function receives the stored string?
- What processing does the function perform?
- What value is finally printed?
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 and can then be passed into program logic.
- A function definition describes processing logic, while a function call applies that logic to a value.
- The palindrome example captures text, reverses it, compares the two versions, and prints the result.
- To debug unexpected output, trace input capture, processing, and output step by step.