Thread: input( )

    #1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    input( )


    Ok i started learning python last night so bear with me here...

    The following code works.. but there is one problem
    when i enter 1 or 2 i get what i programed it to do.. but if i was to just hit enter.. the program gives me an error as follow

    File "./p.py", line 7, in ?
    I = input( "> " )
    File "<string>", line 0

    ^
    SyntaxError: unexpected EOF while parsing

    Code:
    start  =  ["Starting Python..", "Python started!", "Python ended!"]
    
    
    print "What would you like to do?\n1: Start program\n2: End program\n"
    
    I = input( "> " )
    
    if I == 1:
      print "\n",start[0],"\n",start[1],"\n",
      
    elif I == 2:
      print "\n",start[2],"\n" 
      
    else:
      print "Hey enter the damn number man!"
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi xlordt, Firstly welcome to the Python board. Always nice to see a new face!

    Anyway what your looking for here is the raw_input(prompt) function.. input(prompt) not only takes user input but evaluates it as a Python expression i.e. the following Python shell example..

    >>> input('Enter an Expression to be evaulated: ')
    Enter an Expression to be evaulated:
    1 + 1
    2
    >>> raw_input('Enter an Expression to be evaulated: ')
    Enter an Expression to be evaulated:
    1 + 1
    '1 + 1'
    >>>

    Hope this helps,
    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    yes thanx.. the raw_input worked for me.. but what is the diffrence? =)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    As Netytan said, the difference is that, while raw_input() stores a string, input() not only stores the string, but also evaluates it.

    If I would enter "1+1" when prompted by raw_input(), the result would be "1+1". If I entered the same string when prompted by input(), it would store 2 (note the lack of quotes).
    Last edited by percivall; November 2nd, 2003 at 03:11 AM.

IMN logo majestic logo threadwatch logo seochat tools logo