|
User InteractionFunctions and Methods Table Of Contents Strings Page Contents (hide) User interaction is very common in Python programs. We typically display information to the user in the terminal and extract data from the user via the keyboard. As in Java, the terminal represents standard output and the keyboard standard input. Standard InputJava version 1.5 introduced the // Java example Scanner keyboard = new Scanner( System.in ); System.out.println( "What is your name? " ); String name = keyboard.next(); In python, there is a single function for extracting data from the user. The following Python code segment is equivalent to that shown above print "What is your name? " name = raw_input() The User PromptsWhen extracting data from the user, we typically prompt the user with a message indicating what should be entered. Since this is so common, Python allows an argument to be passed to the name = raw_input( "What is your name? " ) When executed, the Python interpreter will display the string on a new line and then wait for the user to enter the data. Numeric ValuesThe userInput = raw_input( "What is your gpa? " ) gpa = float( userInput ) The corresponding Java code would be // Java sample System.out.print( "What is your gpa? " ); double gpa = keyboard.nextDouble(); If the user enters Using the The code segment above can be written as a single statement by nesting the two function calls gpa = float( raw_input( "What is your gpa? " ) ) Standard OutputIn Python, as you have seen in a number of examples, the The avg = grade / 3.0 print avg To display or print multiple values, you can use multiple print "Your average grade = ", avg
The default behavior of the print "GRADE REPORT!" print "Your average grade = ", avg would produce the following output assuming GRADE REPORT! Your average grade = 85.2 You can suppress the linefeed by ending the print "GRADE REPORT!", print "Your average grade = ", avg In this case, the results are printed all on one line since the first GRADE REPORT!Your average grade = 85.2 Example ProgramA sample program is provided which illustrates user interaction. Data is extracted from the user and then used to compute an employee’s take home pay. First, consider the Java version of the program Program: Wages.java File: Wages.java
/* Wages.java * Computes the taxes and wages for an employee given the * number of hours worked and their pay rate. */ import java.util.*; public class Wages { public static void main( String[] args ) { // Set tax rates as constants. final double STATE_TAX_RATE = 0.035; final double FED_TAX_RATE = 0.15; double hours, payRate; double wages, stateTaxes, fedTaxes, takeHome; String employee; Scanner keyboard = new Scanner( System.in ); // Extract data from the user. System.out.print( "Employee name: " ); employee = keyboard.next(); System.out.print( "Hours worked: " ); hours = keyboard.nextDouble(); System.out.print( "Pay rate: " ); payRate = keyboard.nextDouble(); // Compute the employee's taxes and wages. wages = hours * payRate; stateTaxes = wages * STATE_TAX_RATE; fedTaxes = wages * FED_TAX_RATE; takeHome = wages - stateTaxes - fedTaxes; // Print the results. System.out.println( "PAY REPORT" ); System.out.println( "Employee: " + employee ); System.out.println( "----------------------------------" ); System.out.println( "Wages: " + wages ); System.out.println( "State Taxes: " + stateTaxes ); System.out.println( "Fed Taxes: " + fedTaxes ); System.out.println( "Pay: " + takeHome ); } } and compare it to the equivalent Python version Program: wages.py File: wages.py
# wages.py # Computes the taxes and wages for an employee given the # number of hours worked and their pay rate. # Set tax rates as constants. STATE_TAX_RATE = 0.035 FED_TAX_RATE = 0.15 # Extract data from the user. employee = raw_input( "Employee name: " ) hours = float( raw_input( "Hours worked: " ) ) payRate = float( raw_input( "Pay rate: " ) ) # Compute the employee's taxes and wages. wages = hours * payRate stateTaxes = wages * STATE_TAX_RATE fedTaxes = wages * FED_TAX_RATE takeHome = wages - stateTaxes - fedTaxes # Print the results. print "PAY REPORT" print "Employee: ", employee print "----------------------------------" print "Wages: ", wages print "State Taxes: ", stateTaxes print "Fed Taxes: ", fedTaxes print "Pay: ", takeHome
Formatted OutputJava version 1.5 introduced the print "The sum of %d and %d is %f\n" % (a, b, sum) Complete details on the use of the formatting operator is provided in the next chapter. | |||
© 2006 - 2008: Rance Necaise - Page last
modified on September 08, 2006, at 10:38 AM
|