Constantly looking for user input in Python

前端 未结 2 637
醉酒成梦
醉酒成梦 2021-02-11 03:36

How would I write a Python program that would always be looking for user input. I think I would want to have a variable equal to the input and then something different would hap

相关标签:
2条回答
  • 2021-02-11 04:22

    If you want to constantly look for an user input you'll need multithreading.

    Example:

    import threading
    import queue
    
    def console(q):
        while 1:
            cmd = input('> ')
            q.put(cmd)
            if cmd == 'quit':
                break
    
    def action_foo():
        print('--> action foo')
    
    def action_bar():
        print('--> action bar')
    
    def invalid_input():
        print('---> Unknown command')
    
    def main():
        cmd_actions = {'foo': action_foo, 'bar': action_bar}
        cmd_queue = queue.Queue()
    
        dj = threading.Thread(target=console, args=(cmd_queue,))
        dj.start()
    
        while 1:
            cmd = cmd_queue.get()
            if cmd == 'quit':
                break
            action = cmd_actions.get(cmd, invalid_input)
            action()
    
    main()
    

    As you'll see this, will get your messages a little mixed up, something like:

    > foo
    > --> action foo
    bar
    > --> action bar
    cat
    > --> Unknown command
    quit
    

    That's beacuse there are two threads writing to stdoutput at the same time. To sync them there's going to be need of lock:

    import threading
    import queue
    
    def console(q, lock):
        while 1:
            input()   # Afther pressing Enter you'll be in "input mode"
            with lock:
                cmd = input('> ')
    
            q.put(cmd)
            if cmd == 'quit':
                break
    
    def action_foo(lock):
        with lock:
            print('--> action foo')
        # other actions
    
    def action_bar(lock):
        with lock:
            print('--> action bar')
    
    def invalid_input(lock):
        with lock:
            print('--> Unknown command')
    
    def main():
        cmd_actions = {'foo': action_foo, 'bar': action_bar}
        cmd_queue = queue.Queue()
        stdout_lock = threading.Lock()
    
        dj = threading.Thread(target=console, args=(cmd_queue, stdout_lock))
        dj.start()
    
        while 1:
            cmd = cmd_queue.get()
            if cmd == 'quit':
                break
            action = cmd_actions.get(cmd, invalid_input)
            action(stdout_lock)
    
    main()
    

    Ok, now it's better:

        # press Enter
    > foo
    --> action foo
        # press Enter
    > bar
    --> action bar
        # press Enter
    > cat
    --> Unknown command
        # press Enter
    > quit
    

    Notice that you'll need to press Enter before typing a command to enter in "input mode".

    0 讨论(0)
  • 2021-02-11 04:40

    from http://www.swaroopch.com/notes/Python_en:Control_Flow

    #!/usr/bin/python
    # Filename: while.py
    
    number = 23
    running = True
    
    while running:
        guess = int(input('Enter an integer : '))
    
        if guess == number:
            print('Congratulations, you guessed it.')
            running = False # this causes the while loop to stop
        elif guess < number:
            print('No, it is a little higher than that.')
        else:
            print('No, it is a little lower than that.')
    else:
        print('The while loop is over.')
        # Do anything else you want to do here
    
    print('Done')
    
    0 讨论(0)
提交回复
热议问题