high-level "read" function?

David Ascher da at ski.org
Mon Jun 28 21:41:09 CEST 1999


On Mon, 28 Jun 1999, Christine Celata wrote:

> I am just starting to use Python.  It is foreign to me-- I'm a physicist
> used to Fortran, not C.  I have succeeded in getting Python to read
> program user input, but as far as I can see, in order to get it to read
> numbers separated by commas from the terminal and store them as floating
> point variables,

input() can do most of the job for you:

  >>> userinput = input("Enter a bunch of number: ")
  Enter a bunch of number: 1.24, 400, 15.5
  >>> print userinput
  (1.24, 400, 15.5)
  >>> type(userinput)
  <type 'tuple'>
  >>> type(userinput[0])
  <type 'float'>
  >>> type(userinput[1])
  <type 'int'>

If you really need them all to be floating point variables, you can do
that with:

  >>> floats = map(float, userinput)
  >>> print floats
  [1.24, 400.0, 15.5]  

--david ascher





More information about the Python-list mailing list