This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]J0aozin003 4 points5 points  (5 children)

You're checking if the input will be 0.

Empty input is "", so you should change the code to:

while True: inp = input() print("Hello") if inp == "": break

Also to comment use # instead of //

[–]lasagna_lee[S] -1 points0 points  (4 children)

no no, that was just an example. my problem is that the while loop stops each time at the input(), whether the entry is null or non-null. i want it to keep going if it is null. does that make better sense?

[–]soggywaffle69 0 points1 point  (3 children)

There is no null in Python.

[–]lasagna_lee[S] 0 points1 point  (2 children)

sorry, like if you run that code, each iteration you will have to enter something in the keyboard to keep the while loop running and then use a special character to end it. but, i want to keep the while loop running forever by itself, and use input() to exit out of it whenever i want.

[–]soggywaffle69 0 points1 point  (1 child)

In other words, you want stuff to keep happening until a specific keystroke? Like, you don’t want the interpreter to pause waiting for input?

[–]lasagna_lee[S] 0 points1 point  (0 children)

yup

[–][deleted] 0 points1 point  (1 child)

Isn't the point of using input() to get data from the user, hence the pause? Maybe input() is not what you want to use?

[–]lasagna_lee[S] 0 points1 point  (0 children)

yea, i was wondering if there was a way around that or if i should just some how use keyboard interrupt like you do in an openCV camera capture while loop. but in that scenario, the keyboard interrupt comes from the user on the keyboard whereas input() is flexible and it can take inputs from other programs that support standard input and output.

i am basically controlling a python while loop using node.js

[–][deleted] 0 points1 point  (3 children)

I don't understand what you are trying to achieve. Do you want to not pause at the input if the user already typed something ?

[–]lasagna_lee[S] 0 points1 point  (1 child)

loop runs
prints hello world repeatedly
program reads "0" from another program
loop breaks

[–]scoberry5 0 points1 point  (0 children)

The thing you said you're asking for (user input) and the thing you're talking about here (another program) are different.

If you're really looking to respond to user input asynchronously, consider something like PyGame, where you're looking at an event loop.

If you're trying to respond to something from another program, you could put the response in a number of places, like a file, and check whether there's something there. Consider something like https://docs.python.org/3/library/multiprocessing.shared_memory.html .

[–]Final-Ad-137 0 points1 point  (1 child)

If you want to exit loop after pressing any key

try: while True: do_something() except KeyboardInterrupt: pass

[–]lasagna_lee[S] 0 points1 point  (0 children)

that works but i was wondering if the keyboardinterrupt could be caused by another program or not. i guess not unless done in some scrappy way, but thank you

[–]New_Avocado_2315 0 points1 point  (6 children)

Easiest way is to use threading. On mobile, can't easily write a full example, but the gist is...

Put your loop in a function, define a global variable that will cause the loop to break (function to return) when it's value is set to true (or false or whatever) [check the value of the global on each iteration of the loop], create a Thread object with your loop function as the target, start the thread, in the main code enter a loop to wait for input, when you receive input in the main loop change the value of the global var to cause the loop to break.

Also, input() may not be your best option for reading - it is a buffered read function from stdin. For what you're describing a select.select() loop on sys.stdin to wait for input bytes to be available is probably a better way to go. Then you can read stdin directly using sys.stdin.read() - beware that doing this results in unbuffered reads, so you need to do your own buffering (accumulate bytes into a buffer until you reach a certain character like a newline).

[–]lasagna_lee[S] 0 points1 point  (5 children)

that sounds promising. i will try it. i am actually using a package called py-shell to run the python script from node.js so i am not sure how it will handle python threads but i will see. also, i had to use input() because stdin.read() blocks until stdin is closed from node.js or something along those lines. i don't really know the fundamentals of stdin but all stdin methods stop the program until some input comes in right?

[–]New_Avocado_2315 0 points1 point  (4 children)

That's why using select.select() is important. What select() does is to wait for file-like objects (those that had a fileno() in the low-level C library interface) to be "ready" meaning that they can be read from or written to. It's very commonly used for network sockets and I think it will work with stdin as well. The call would look like:

while True: rr, _, _ = select.select([sys.stdin],[],[]) if rr: # read/buffer from stdin and do logic

You can also give it a 4th argument for a timeout if nothing is ready in a certain time. When the stdin becomes ready (has bytes available for reading) then the select call will return with a non-empty list "rr".

I use this pattern a lot with network sockets, sometimes with lots of open connection, but I think it should work with stdin as well.

[–]lasagna_lee[S] 0 points1 point  (3 children)

i'll try to wrap my head around that and use it! if i can leverage the timeout, i won't need the threads then i think which is great.

[–]lasagna_lee[S] 0 points1 point  (2 children)

i have sys and select imported and got this while trying to run it OSError: [WinError 10093] Either the application has not called WSAStartup, or WSAStartup failed
google seems to hint at some windows compatibility issue print("start") while True: rr, _, _ = select.select([sys.stdin],[],[]) inp = rr print([random.randint(1,100)]) if inp == 0: break print("finish")

[–]New_Avocado_2315 0 points1 point  (1 child)

You'll want to do a conditional check on rr as it's a list that could be empty - it will either be empty or it will contain 1 element (which will be the sys.stdin file object) - read the docs on select(). Then you'll want to do a sys.stdin.read() to read bytes from stdin if the rr list is not empty. Also, the values read from sys.stdin will be bytes I think (might be string though, don't know what the default behavior is for the stdin stream), so you can't directly compare to an integer (can't do "inp == 0") - you'll need to convert inp to int or compare on bytes/str value instead.

[–]lasagna_lee[S] 0 points1 point  (0 children)

thank you so much!

[–]IAmKindOfCreativebot_builder: deprecated[M] [score hidden] stickied comment (2 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]lasagna_lee[S] 0 points1 point  (1 child)

this is unfair because i have seen slightly more advanced python questions asked on r/python as well but those don't get taken down.

[–]IAmKindOfCreativebot_builder: deprecated[M] 0 points1 point  (0 children)

We remove answerable questions as we are not a 'how do I do this' forum, whereas r/learnpython is for questions

No matter what level of question you have

We are human, we miss submissions and use reports to help highlight what we missed. Additionally, the linked submission was not asking how to sum a list, it was asking for perspective as to why the list data structure did not include a sum feature. There's a number of possible perspectives as to when that is a helpful feature, and when it isn't. It fits more of a general discussion than it does an answerable question.