Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7416049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:24:51+00:00 2026-05-29T07:24:51+00:00

How would I write a Python program that would always be looking for user

  • 0

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 happen based on what that variable equaled. So if the variable were “w” then it would execute a certain command and keep doing that until it received another input like “d” Then something different would happen but it wouldn’t stop until you hit enter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:24:51+00:00Added an answer on May 29, 2026 at 7:24 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a cross-platform python program that would run in the
I want to write a program that would print every combination of a set
I would like to write some scripts in python that do some automated changes
In Python, I'd like to write a function that would pretty-print its results to
There are many ways to write a Python program that computes a histogram. By
I am trying to write a python program using Python 3 I have to
I have a python program that does something like this: Read a row from
I'm very new to Python and trying to write a program that parses some
In a Python program I have a read-only property that I create using bla
I have a python program that is essentailly made up of turtle graphics, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.