Thread: Loop Problem

    #1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Loop Problem


    I have some code but I can't figure out how to work them both at the same time.
    Code:
    while True:
        parse_data(s.recv(1024))
    while 1 == 1:
        Wait(3)
        input = raw_input("")
        Send(input)
    At the correct time parse_data(s.recv(1024)) will print onto the main interface, messages or users joining a channel. While at the same time there should be an input space on the programs main interface where you can type and click enter to send (Send(input)) a message to battle.net. Thanks.
    Edit: Wait() is time.sleep().
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can't actually run two loops at the same time... without threading, and even then you could argue that only one thing is happening at a time . But this isn't what you want anyway.

    All you should need to do is put the second loop inside the first. This way, the program will enter an infite loop: inside this loop parse_data() will be called before executing our inner loop. When the inner loop finishes parse_data() will be called again, and around we go.

    You should of course exit the outer [infinite] loop at some point so that the program can end .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11
    Code:
    while True:
        parse_data(s.recv(1024))
    while 1 == 1:
        Wait(3)
        input = raw_input("")
        Send(input)
    Also you should find another name than "input" here:

    Code:
    input = raw_input("")
    since input is a builtin function which can not be used as this builtin function after it has been converted to a string by raw_input user input:


    Code:
    >>> input
    <built-in function input>
    >>> input("> ")
    > 5
    5
    >>> input = raw_input("> ")
    > hi
    >>> input
    'hi'
    >>> input("> ")
    Traceback (most recent call last):
      File "<pyshell#10>", line 1, in ?
        input("> ")
    TypeError: 'str' object is not callable
    >>>
    Dag
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Hmm, I've been using input, as long as I'm not using the while True statement, it works just fine. I decided to get rid of the Send message part, my bot is more for safe guarding channels, people don't *need* to talk through it as well.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just a thought but you might want to look at the asyncore and asynchat modules if you haven't already.

    http://www.python.org/doc/2.4/lib/module-asyncore.html
    http://www.python.org/doc/2.4/lib/module-asynchat.html

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo