Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are multiple separate problems with this code:</p> <pre><code>while run: # loop through each line of user input, adding it to buffer for line in sys.stdin.readlines(): if line == 'quit': run = False </code></pre> <hr> <p>First, you have an inner loop that won't finish until all lines have been processed, even if you type "quit" at some point. Setting <code>run = False</code> doesn't break out of that loop. Instead of quitting as soon as you type "quit", it will keep going until it's looked at all of the lines, and <em>then</em> quit if you typed "quit" at any point.</p> <p>You can fix this one pretty easily by adding a <code>break</code> after the <code>run = False</code>.</p> <hr> <p>But, with or without that fix, if you <em>didn't</em> type "quit" during that first time through the outer loop, since you've already read all input, there's nothing else to read, so you'll just keep running an empty inner loop over and over forever that you can never exit.</p> <p>You have a loop that means "read and process all the input". You want to do <em>that</em> exactly once. So, what should the outer loop be? It should not be anyway; the way to do something once is to not use a loop. So, to fix this one, get rid of <code>run</code> and the <code>while run:</code> loop; just use the inner loop.</p> <hr> <p>Then, if you type "quit", <code>line</code> will actually be <code>"quit\n"</code>, because <code>readlines</code> does not strip newlines.</p> <p>You fix this one by either testing for <code>"quit\n"</code>, or <code>strip</code>ping the lines.</p> <hr> <p>Finally, even if you fix all of these problems, you're still waiting forever before doing anything. <code>readlines</code> returns a <code>list</code> of lines. The only way it can possibly do that is by reading all of the lines that will ever be on <code>stdin</code>. You can't even start looping until you've read all those lines.</p> <p>When standard input is a file, that happens when the file ends, so it's not <em>too</em> terrible. But when standard input is the Windows command prompt, the command prompt never ends.* So, this takes forever. You don't get to start processing the list of lines, because it takes forever to wait for the list of lines.</p> <p>The solution is to not use <code>readlines()</code>. Really, there is never a good reason to call <code>readlines()</code> on anything, <code>stdin</code> or not. Anything that <code>readlines</code> works on is <em>already</em> an iterable full of lines, just like the <code>list</code> that <code>readlines</code> would give you, except that it's "lazy": it can give you the lines one at a time, instead of waiting and giving you all of them at once. (And even if you really need the list, just do <code>list(f)</code> instead of <code>f.readlines()</code>.)</p> <p>So, instead of <code>for line in sys.stdin.readlines():</code>, just do <code>for line in sys.stdin:</code> (Or, better, replace the explicit loop completely and use a sequence of iterator transformations, as in mgilson's answer.)</p> <hr> <p>The fixes JBernardo, Wing Tang Wong, etc. proposed are all correct, and necessary. The reason none of them fixed your problems is that if you have 4 bugs and fix 1, your code still doesn't work. That's exactly why "doesn't work" isn't a useful measure of anything in programming, and you have to debug what's actually going wrong to know whether you're making progress.</p> <hr> <p>* I lied a bit about <code>stdin</code> never being finished. If you type a control-Z (you may or may not need to follow it with a return), then <code>stdin</code> is finished. But if your assignment is to make it quit as soon as the user types "quit"&lt; turning in something that only quits when the user types "quit" and then return, control-Z, return again probably won't be considered successful.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload