raw_input on several lines

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TP

    raw_input on several lines

    Hi everybody,

    When using raw_input(), the input of the user ends when he types Return on
    his keyboard.
    How can I change this behavior, so that another action is needed to stop the
    input? For example, CTRL-G. It would allow the user to input several lines.

    Thanks

    Julien


    --
    python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+ ,\'Z
    (55l4('])"

    "When a distinguished but elderly scientist states that something is
    possible, he is almost certainly right. When he states that something is
    impossible, he is very probably wrong." (first law of AC Clarke)
  • Larry Bates

    #2
    Re: raw_input on several lines

    TP wrote:
    Hi everybody,
    >
    When using raw_input(), the input of the user ends when he types Return on
    his keyboard.
    How can I change this behavior, so that another action is needed to stop the
    input? For example, CTRL-G. It would allow the user to input several lines.
    >
    Thanks
    >
    Julien
    >
    >
    Just put raw_input() in a loop and check for something. Actually a blank line
    would probably work best in that case. Not tested, but you will get the idea:

    lines = list()
    print 'Enter your text (empty line to quit)'
    while 1:
    line = raw_input('')
    if line.strip() == '':
    break

    lines.append(li ne)



    -Larry

    Comment

    • Stefaan Himpe

      #3
      Re: raw_input on several lines

      How can I change this behavior, so that another action is needed to stop the
      input? For example, CTRL-G. It would allow the user to input several lines.
      >
      I don't think you can change raw_input's behaviour in this respect, but
      you could build something yourself that's based on interpretation of raw
      keyboard scan codes.

      Google is your friend in this...

      e.g. on Linux you could use something like urwid
      e.g. on win32 you could use something like


      I am not aware of an os independent way to accomplish what you want.

      Comment

      • David

        #4
        Re: raw_input on several lines

        TP wrote:
        Hi everybody,
        >
        When using raw_input(), the input of the user ends when he types Return on
        his keyboard.
        How can I change this behavior, so that another action is needed to stop the
        input? For example, CTRL-G. It would allow the user to input several lines.
        >
        Thanks
        >
        Julien
        >
        >
        >
        How about;

        student_scores = {}
        name = raw_input("Plea se enter a student name (q quits): ")
        while(name != 'q'):
        score = input("Please enter the students score: ")
        student_scores[name] = score
        name = raw_input("Plea se enter a student name (q quits): ")
        for current_key in student_scores. keys():
        print "The current students are:", current_key



        --
        Powered by Gentoo GNU/LINUX


        Comment

        • Raja Baz

          #5
          Re: raw_input on several lines

          On Sat, 02 Aug 2008 21:58:09 +0200, TP wrote:
          Hi everybody,
          >
          When using raw_input(), the input of the user ends when he types Return
          on his keyboard.
          How can I change this behavior, so that another action is needed to stop
          the input? For example, CTRL-G. It would allow the user to input several
          lines.
          >
          Thanks
          >
          Julien
          Well I don't know about using CTRL-G. Now I'm pretty sure you can't
          change the behavior of raw_input() like this *but* what you do is the
          following:
          >>from sys import stdin
          >>user_input = stdin.readlines ()
          this
          is
          a multiline
          test
          >>user_input
          ['this\n', 'is\n', 'a multiline\n', 'test\n']
          >>>
          The end of the input is marked by the "End of Transmission" or "End of
          File" character(which can be obtained via ctrl+D, at least on linux, I
          have no idea about win32)
          This would have the additional bonus of working with something being
          piped into it as an input

          Comment

          Working...