0

I am trying to wait for user input. If no user input is provided then break. I found this code useful, but the problem is that it will continue if there was no user input provided. Is there something else I can add to make it break instead of continue?

The use case

I am sending a test email to myself. if the test email looks good I will send the email to the targeted people. I have two functions, one for the test and one for the real email. and between them, I need a break in case the test email has a problem.

import select
import sys

print('Press enter to continue.', end='', flush=True)
r, w, x = select.select([sys.stdin], [], [], 600)
print("the code will keep going after 600 seconds pass")
engzaz
  • 76
  • 5
  • Does this answer your question? [Keyboard input with timeout?](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout) – PM 77-1 Mar 02 '22 at 21:19
  • Have you tried `input()`? – Mateen Ulhaq Mar 02 '22 at 21:20
  • Where do you want to break from ? Will this be inside a loop ? – Stijn B Mar 02 '22 at 21:31
  • I am sending a test email to myself. if the test email looks good I will send the email to the targeted people. I have two functions, one for the test and one for the real email. and between them, I need a break in case the test email has a problem, – engzaz Mar 02 '22 at 21:43

3 Answers3

0

I would use input

input('Press enter to continue.')
rowBee
  • 61
  • 1
  • 5
  • That is what I am planning to do if select/sys doesn't provide a way to break the script if the user didn't click on enter – engzaz Mar 02 '22 at 21:33
0

based on python documentation I don't see an option to break after the time out [https://docs.python.org/3/library/select.html][1]

select.select(rlist, wlist, xlist[, timeout])
engzaz
  • 76
  • 5
0

In answer to your comment (wish is in my opinion way more understandable than the initial question) :

I am sending a test email to myself. if the test email looks good I will send the email to the targeted people. I have two functions, one for the test and one for the real email. and between them, I need a break in case the test email has a problem

import sys

send_mail_to_me()

answer = input("Send to everybody? Y/n")
if answer == "Y":
    send_mail_to_everybody()
else:
    sys.exit()

sys.exit() will terminate the Python script. See this for detailed information

Stijn B
  • 350
  • 2
  • 12