Python 3.7.1 try except block happening in wrong order in command line












1















I'm a beginner and working on a very basic skill set.



I'm making a simple text game in command line for windows, and have a function that lets users read the most recent statement, and skip it by causing a KeyboardInterrupt like with Ctrl-C.



from time import sleep
def wait(seconds):
try:
sleep(seconds)
except KeyboardInterrupt:
pass
return


the issue arises when I want to print something and not have a newline afterwards. In that case, the wait() function will execute before the print() function



# functions properly, but has unwanted newline
print("test", end='testn')
wait(3)
# in windows CMD, wait() executes before print()
print("test", end='test')
wait(3)


I know there are ways around this like using TKinter, but I want to know why this happens, not how to avoid it entirely.



EDIT: I kept searching and found the issue wasn't the try except block, but sleep():
Error with Print and Sleep in Python
copy of the answer:



You should use:



print (a, end="", flush=True)


Because console output is line-buffered.










share|improve this question

























  • I can't replicate this with your example. With both ways, n and no n it executes in the expected order, print then wait, for me.

    – lieblos
    Nov 19 '18 at 3:21













  • I was reading and was about to answer, then saw that you'd already found the answer :). Since you figured it out, you should post an answer to your question; it's very much allowed! Welcome to Stack Overflow!

    – Cyphase
    Nov 19 '18 at 6:25
















1















I'm a beginner and working on a very basic skill set.



I'm making a simple text game in command line for windows, and have a function that lets users read the most recent statement, and skip it by causing a KeyboardInterrupt like with Ctrl-C.



from time import sleep
def wait(seconds):
try:
sleep(seconds)
except KeyboardInterrupt:
pass
return


the issue arises when I want to print something and not have a newline afterwards. In that case, the wait() function will execute before the print() function



# functions properly, but has unwanted newline
print("test", end='testn')
wait(3)
# in windows CMD, wait() executes before print()
print("test", end='test')
wait(3)


I know there are ways around this like using TKinter, but I want to know why this happens, not how to avoid it entirely.



EDIT: I kept searching and found the issue wasn't the try except block, but sleep():
Error with Print and Sleep in Python
copy of the answer:



You should use:



print (a, end="", flush=True)


Because console output is line-buffered.










share|improve this question

























  • I can't replicate this with your example. With both ways, n and no n it executes in the expected order, print then wait, for me.

    – lieblos
    Nov 19 '18 at 3:21













  • I was reading and was about to answer, then saw that you'd already found the answer :). Since you figured it out, you should post an answer to your question; it's very much allowed! Welcome to Stack Overflow!

    – Cyphase
    Nov 19 '18 at 6:25














1












1








1








I'm a beginner and working on a very basic skill set.



I'm making a simple text game in command line for windows, and have a function that lets users read the most recent statement, and skip it by causing a KeyboardInterrupt like with Ctrl-C.



from time import sleep
def wait(seconds):
try:
sleep(seconds)
except KeyboardInterrupt:
pass
return


the issue arises when I want to print something and not have a newline afterwards. In that case, the wait() function will execute before the print() function



# functions properly, but has unwanted newline
print("test", end='testn')
wait(3)
# in windows CMD, wait() executes before print()
print("test", end='test')
wait(3)


I know there are ways around this like using TKinter, but I want to know why this happens, not how to avoid it entirely.



EDIT: I kept searching and found the issue wasn't the try except block, but sleep():
Error with Print and Sleep in Python
copy of the answer:



You should use:



print (a, end="", flush=True)


Because console output is line-buffered.










share|improve this question
















I'm a beginner and working on a very basic skill set.



I'm making a simple text game in command line for windows, and have a function that lets users read the most recent statement, and skip it by causing a KeyboardInterrupt like with Ctrl-C.



from time import sleep
def wait(seconds):
try:
sleep(seconds)
except KeyboardInterrupt:
pass
return


the issue arises when I want to print something and not have a newline afterwards. In that case, the wait() function will execute before the print() function



# functions properly, but has unwanted newline
print("test", end='testn')
wait(3)
# in windows CMD, wait() executes before print()
print("test", end='test')
wait(3)


I know there are ways around this like using TKinter, but I want to know why this happens, not how to avoid it entirely.



EDIT: I kept searching and found the issue wasn't the try except block, but sleep():
Error with Print and Sleep in Python
copy of the answer:



You should use:



print (a, end="", flush=True)


Because console output is line-buffered.







python python-3.x printing try-except






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 3:29







Defending Towers

















asked Nov 19 '18 at 3:11









Defending TowersDefending Towers

63




63













  • I can't replicate this with your example. With both ways, n and no n it executes in the expected order, print then wait, for me.

    – lieblos
    Nov 19 '18 at 3:21













  • I was reading and was about to answer, then saw that you'd already found the answer :). Since you figured it out, you should post an answer to your question; it's very much allowed! Welcome to Stack Overflow!

    – Cyphase
    Nov 19 '18 at 6:25



















  • I can't replicate this with your example. With both ways, n and no n it executes in the expected order, print then wait, for me.

    – lieblos
    Nov 19 '18 at 3:21













  • I was reading and was about to answer, then saw that you'd already found the answer :). Since you figured it out, you should post an answer to your question; it's very much allowed! Welcome to Stack Overflow!

    – Cyphase
    Nov 19 '18 at 6:25

















I can't replicate this with your example. With both ways, n and no n it executes in the expected order, print then wait, for me.

– lieblos
Nov 19 '18 at 3:21







I can't replicate this with your example. With both ways, n and no n it executes in the expected order, print then wait, for me.

– lieblos
Nov 19 '18 at 3:21















I was reading and was about to answer, then saw that you'd already found the answer :). Since you figured it out, you should post an answer to your question; it's very much allowed! Welcome to Stack Overflow!

– Cyphase
Nov 19 '18 at 6:25





I was reading and was about to answer, then saw that you'd already found the answer :). Since you figured it out, you should post an answer to your question; it's very much allowed! Welcome to Stack Overflow!

– Cyphase
Nov 19 '18 at 6:25












1 Answer
1






active

oldest

votes


















0














I agree with your answer. You should use:



print(a, end="", flush=True)


because console output is line-buffered.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367807%2fpython-3-7-1-try-except-block-happening-in-wrong-order-in-command-line%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I agree with your answer. You should use:



    print(a, end="", flush=True)


    because console output is line-buffered.






    share|improve this answer




























      0














      I agree with your answer. You should use:



      print(a, end="", flush=True)


      because console output is line-buffered.






      share|improve this answer


























        0












        0








        0







        I agree with your answer. You should use:



        print(a, end="", flush=True)


        because console output is line-buffered.






        share|improve this answer













        I agree with your answer. You should use:



        print(a, end="", flush=True)


        because console output is line-buffered.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 6:57









        J_HJ_H

        3,3501617




        3,3501617






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367807%2fpython-3-7-1-try-except-block-happening-in-wrong-order-in-command-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)