How to use raw_input() in python

Asked 2 years ago, Updated 2 years ago, 46 views

I'm a python beginner. I'd like to get input() or raw_input() from the console in the while loop, but if there's no input, I'd like to write a code that runs through the next command. (I'd like to implement input() if there's nothing in the console buffer.)
As an image,

while True:
    tmp = '0'
    if(console.readable()):
        tmp = input()
        print tmp

    hogehoge

That's what it looks like. (This code is just an image, so it doesn't actually work.)
This part of console.readable() was implemented when something similar was done in C before, but the question is how to implement this part in python.
I can't find a good page to look into, so could someone help me?

Thank you for your cooperation

python

2022-09-30 21:31

3 Answers

You may be able to change the console mode or use OS-dependent module features to make input() non-block operation, but I thought about the following implementation: Input.input() works in block mode by default and works in block mode.

import threading
import queue
import time


class Input (threading.Thread):
    def__init__(self):
        super().__init__(daemon=True)
        self.queue = queue.Queue()
        self.start()

    def run(self):
        while True:
            t = input()
            self.queue.put(t)

    default input (self, block=True, timeout=None):
        try:
            return self.queue.get (block, timeout = timeout)
        except queue.Empty:
            return None


defmain():
    sin=Input()
    for i in range (10):
        time.sleep(1)#Something to do
        t=cin.input(block=False)
        print('{}:t={}.format(i,t))


if__name__=='__main__':
    main()

(Tried Windows 10 cygwin 64-bit Python 3.6.4, Ubuntu 16.04 LTS Python 3.5.2)

However, if you move it on cygwin, the stack trace will stop displaying when an exception occurs.If I press ENTER several times, it will be displayed until the end and will return to the shell.It didn't happen in WSL or Ubuntu.I think this implementation is influenced by console behavior.

I'm sorry that the implementation was a little half-baked, but I made a comment as an idea.


2022-09-30 21:31

I think this question is about Python's input(), which is supposed to wait for input data, but is it possible to achieve the same specification as getchar() in C language?The answer is that Python's standard library alone is not enough to achieve this feature.

It's easy to implement with a window application.In the window application, for example, if it's a game, I think it's normal to move characters with arrow keys.For information on obtaining key events in the window application, please refer to the following:

http://www.geocities.jp/penguinitis2002/computer/programming/Python/PyGTK/03-key.html

If you really want to do it with the console app, it's a little bit of a hassle unlike keystrokes, but you can do it by using a file.Instead of console.readable(),

os.path.isfile('a.txt')

You can do it by writing it to a file from the terminal as follows:

echo'a'>a.txt


2022-09-30 21:31

The questioner doesn't seem to be visiting here anymore, but as an example of know-how.
If you want to do it on the console, there is a library called curses.

curses--- Terminal operations to handle character cell display
Programming Curses in Python
windows-curses 1.0
Curses programming in Python

User input
The curses library itself provides only a very simple input mechanism, and Python has added a widget to enter a string to compensate for this defect.

The most commonly used way to get input into a window is to use the getch() method in the window.This pauses, waits for the user to enter a key, and displays it if echo() was previously called.You can also specify additional coordinates to move the cursor before pausing.

You can change this behavior with the nodelay() method.After nodelay(1), getch() against the window will go non-blocking and return ERR(-1) if there is no input.There is also a halfdelay() function, which allows you to set a time limit for each getch(); curses generates an exception if the input is not available after the millisecond specified by the halfdelay() argument.

The getch() method returns an integer; if the return value is between 0 and 255, it represents the ASCII code of the pressed key.A value greater than 255 represents a special key, such as Page Up, Home, or cursor keys.You can compare the return value with constants such as curses.KEY_PPAGE, curses.KEY_HOME, curses.KEY_LEFT.Typically, your main loop will look like this:

while1:
    c=stdscr.getch()
    ifc==ord('p')—PrintDocument()
    elifc==ord('q'):break#Exit the while()
    elifc==curses.KEY_HOME: x=y=0

The curses.ascii module provides an ASCII class class function that takes an integer or a single character string as an argument; this is useful for writing easy-to-read tests for your command interpreter.The class also provides a function that takes an integer or a single character string as an argument and converts it to each other.For example, curses.ascii.ctrl() returns the control character corresponding to the argument.


2022-09-30 21:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.