如何解决Python中input()在一段时间内为空或空时如何执行
我为我想要的选择写了一个 def(),这是我的代码:
def choice():
choices = int(input("""
do you wanna update your database:
1. Yes
2. No
"""))
if choices == 1:
run()
if choices == 2:
run_without_update()
我想知道我是否可以这样做:
def choice():
try:
choices = int(input("""
do you wanna update your database:
1. Yes
2. No
"""))
if choices == 1:
run()
if choices == 2:
run_without_update()
except:
除非选择在 30 秒内没有输入任何内容,然后执行 run()。
我不是一个技术娴熟的家伙,你介意给我一些帮助吗?谢谢大家!
感谢@itprorh66,问题已解决:
在控制台中:pip install inputtimeout
from inputimeout import inputimeout,TimeoutOccurred
def choice():
try:
choices = int(inputimeout(prompt="""
do you wanna update your database:
1. Yes
2. No
""",timeout=10)) # wait as long as you feel comfortable with.
if choices == 1:
run()
else:
run_without_update()
except TimeoutOccurred:
run() # or whatever you want anything to happen.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。