Python 3.7.1 try except block happening in wrong order in command line
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
add a comment |
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
I can't replicate this with your example. With both ways,n
and non
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
add a comment |
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
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
python python-3.x printing try-except
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 non
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
add a comment |
I can't replicate this with your example. With both ways,n
and non
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
add a comment |
1 Answer
1
active
oldest
votes
I agree with your answer. You should use:
print(a, end="", flush=True)
because console output is line-buffered.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
I agree with your answer. You should use:
print(a, end="", flush=True)
because console output is line-buffered.
add a comment |
I agree with your answer. You should use:
print(a, end="", flush=True)
because console output is line-buffered.
add a comment |
I agree with your answer. You should use:
print(a, end="", flush=True)
because console output is line-buffered.
I agree with your answer. You should use:
print(a, end="", flush=True)
because console output is line-buffered.
answered Nov 19 '18 at 6:57
J_HJ_H
3,3501617
3,3501617
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I can't replicate this with your example. With both ways,
n
and non
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