Book » User Interaction »

 


Variables Book Library Modules


User Interaction

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 Input

Java version 1.5 introduced the Scanner class for extracting data from standard input. It provided a number of methods for extracting different types of data. In python, there is a single function for extracting data from the user

  print "What is your name? "
  name = raw_input()

The raw_input() function waits for the user to enter a line of data at the keyboard terminated with a carriage return. The data entered by the user is then returned as a string. In this example, if the user enters Smith at the keyboard, name will be assigned the string Smith.

User Prompts

When 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 raw_input() function to be used as a prompt. We can rewrite the previous example as

  name = raw_input( "What is your name?" )

When executed, the Python interpreter will display the string on a new line and then way for the user to enter the data.

Numeric Values

The raw_input() function can only extract strings. But what if we need to extract an integer or real value? In that case, the numeric value will be stored as a string of digits and we must convert it to a numeric value using the appropriate type conversion function. In the following example, we extract a floating-point value from the user by using the float function to convert the input string to a real value

  input = raw_input( "What is your gpa?" )
  gpa = float( input )

This code segment can be written as a single statement by nesting the two function calls

  gpa = float( raw_input( "What is your gpa?" ) )

Type Conversions

In Java, some data types are automatically converted to a different type depending on how it is used. For example, the value of the integer variable result will be converted automatically to a string when performing the string concatenation

  int result = 40;
  System.out.print( "The result = " + result );

While Python can promote and handle conversions between the numeric data types, it does not automatically convert between other data types. Since there are times when you need to convert between the different primitive data types, Python provides several built-in conversion functions. In the following example, the float function is used to convert the numeric value stored in aString to a numeric value.

  aString = "123.45"
  value = float( aString )

The following is a list of Python’s built-in type conversion functions for strings and numeric types. Each o function creates a new object of the indicated type.

FunctionDescription
int(x)Converts a string or number object to an integer. Floating point numbers are truncated, not rounded.
float(x)Converts a string or number object to a floating point object.
long(x)Converts a string or number to a long integer.
complex(x,y)Creates a complex object with a real part x and an imaginary part y
str(x)Translates object x to a string.
hex(x)Converts the integer or long integer object to a hexadecimal string representation.
oct(x)Converts the integer or long integer object to an octal string representation.
chr(x)Creates a string for the character whose ASCII code is x

Standard Output

In Python, as you have seen in a number of examples, the print statement1 is used to display information to the terminal.

The print statement can be used to display values of any of the primitive types. Internally, Python handles the translation from the primitive type to a string in order for the value to be printed.

  avg = grade / 3.0
  print avg

To display or print multiple values, you can use multiple print statements or separate each value with a comma. In the latter case, Python will display the values separated with spaces.

  print "Your average grade =", avg
Note: You can not concatenate non-string values with strings as can be done in Java.

Formatted Output

Java version 1.5 introduced the printf() method for use with the System.out object to produce formatted output. Python also supports formatted output using the special formatting operator (%) with the print statement.

  print "The sum of %d and %d is %d\n" % (a, b, sum)

Complete details on the use of the formatting operator is provided in a later section.

---

1
© 2006 - 2008: Rance Necaise - Page last modified on August 09, 2006, at 10:04 PM