Printing Output to the Screen
When output is unexpected, do not inspect only the final print statement.
From Prompt to Stored Text
A program can communicate with a person in two directions: it can display a prompt, and it can receive a response. The raw_input() function connects the keyboard to the program. It displays a prompt, waits for the user to type and press Enter, and returns the typed text as a string.
For the statement something = raw_input("Enter text: "), the events occur in order. First, the prompt string is displayed. Next, the program waits. The user types a response and presses Enter. raw_input() captures that response and returns it. Finally, the returned text is stored in the variable something.
The String Boundary
raw_input() captures text from the keyboard and returns it as a string to your program.
The value returned by raw_input() is text, even when the user types characters that look like a number. If your program needs numeric operations, convert the returned string to int or float before performing those operations.
A Complete Palindrome Check
A palindrome is a word or phrase that reads the same forwards and backwards. A program can check one by reversing the captured string and comparing the reversed version with the original. The example below follows the source's three-part design: reverse() flips the string, is_palindrome() compares the two versions, and the main program captures input 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."- 1The function
reverse()receives text and returns that text in reverse order. - 2The function
is_palindrome()callsreverse()and compares the returned string with the original text. - 3The
raw_input()call displays its prompt, waits for input, and stores the returned string in text. - 4The if statement sends the stored text to
is_palindrome(). - 5The program prints one of two results according to the comparison.
reverse()uses slice notation [::-1] to flip the string backwards.is_palindrome()compares the original text with its reversed version.raw_input()captures the user's text and stores it in text.- The if statement selects which result to print.
The displayed result depends on the text entered by the user.Tracing a Palindrome Response
Trace the stages of the palindrome checker when the user enters a palindrome.
- 1Input capture
The prompt is displayed. The user types text and presses Enter.
raw_input()returns that response as a string and stores it in text. - 2Palindrome checking
is_palindrome()receives the stored string,reverse()produces the backwards version, and the two strings are compared. - 3Branch selection
If the original and reversed strings match, the condition is true and the first print statement runs.
- 4Output
The program displays the message that the text is a palindrome.
The response travels from raw_input() to the variable, then through the checking functions, and finally to the selected output.
Debugging by Stages
When output is unexpected, do not inspect only the final print statement. Trace the data step by step and ask what the program knows at each stage. For an input-based program, the important stages are input capture, data processing, and output. The unexpected result must come from one of these stages.
Trace the execution in order: confirm what the user entered, confirm the value passed into the processing logic, and then confirm which output branch ran. This separates an input problem from a processing problem and an output problem.
Common mistake Treating the result of raw_input() as a number automatically
What it looks likeA user types characters that look like a number, but raw_input() still returns text.
Why it failsraw_input()returns a string, so numeric operations require conversion.Do this insteadConvert the returned string to int or float when numeric operations are needed.
Common mistake Debugging only the final output line
What it looks likeThe program prints an unexpected result, so attention goes straight to print.
Why it failsThe divergence may have occurred during input capture or processing.
Do this insteadTrace input capture, processing, and output step by step.
Common mistake Ignoring whitespace or case when comparing text
What it looks likeThe entered text is processed as a string, but its spaces or letter case differ from what the logic expects.
Why it failsString comparisons depend on the text being compared.
Do this insteadHandle whitespace and case sensitivity carefully and test with real input.
Practice the Data Flow
Write down the four stages for a program that uses raw_input() to receive text and then prints a result based on that text. For each stage, state what the program is doing and what value is available.
Hints
- Begin with the prompt and the program waiting.
- Identify the point at which the user's response becomes a string in a variable.
- Separate the program's processing logic from its final output.
Extend the palindrome-checking idea with a test plan. Choose text inputs that let you check a palindrome, a non-palindrome, and text whose whitespace or letter case needs careful consideration. For each input, predict which output branch should run, then trace the actual execution stages.
Hints
- Check the captured string before examining the comparison.
- Compare the original text with the result of reversing it.
- If the result differs from your prediction, identify whether the divergence occurred during capture, processing, or output.
Key Takeaways
- 1
raw_input()displays a prompt, waits for keyboard input, and returns the response as a string. - 2The returned string can be stored in a variable and passed to program logic.
- 3Convert the string to int or float when numeric operations are required.
- 4Input-based programs can be understood and debugged by tracing capture, processing, and output in order.
- 5A complete program can use captured text, apply logic such as a palindrome comparison, and print the selected result.
At a glance
raw_input()is the gateway from the user's keyboard into the program.- The value returned by
raw_input()is a string and may need conversion for numeric operations. - A prompt-and-response program moves through input capture, processing, and output.
- Tracing those stages helps locate the point where actual behavior diverges from expectations.
- The palindrome checker demonstrates how captured text can be passed through functions and used to select printed output.