[英]How would I make it so I can ask the user at the start of this program in the want to execute it because the below code brings back syntax errors
from pynput.keyboard import Key, Listener
import logging
import sys
#Asks User if they want to run program
def yes_or_no():
inp = input("Do You want to log every keystoke Y/N?")
if inp == "Y":
print("Running Program")
#Runs Keylog Program
logging.basicConfig(filename=("keylog.txt"), level=logging.DEBUG, format=" %(asctime)s - %(message)s")
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener :
listener.join()
#Ends script
elif inp == "N":
print("Ending Program")
sys.exit()
else:
print("You must choose between Y or N")
return yes_or_no()
yes_or_no()
So the elif statement wont allow I believe it has something to do with the while statement.所以 elif 语句不允许我相信它与 while 语句有关。 When run it should open up a console like window and ask the Question "Would you like to log every input Y/N".
运行时,它应该打开一个类似窗口的控制台并询问问题“你想记录每个输入 Y/N”。 I does not do this and I also get the syntax error
我不这样做,我也得到语法错误
The code I started with我开始的代码
from pynput.keyboard import Key, Listener
import logging
logging.basicConfig(filename=("keylog.txt"), level=logging.DEBUG, format=" %(asctime)s - %(message)s")
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener :
listener.join()
I suspect this is an indentation issue, however please be clear on your problem what is the output you want, do you want a closure function?我怀疑这是一个缩进问题,但是请清楚你的问题你想要什么输出,你想要一个闭包函数吗?
from pynput.keyboard import Key, Listener
import logging
import sys
#Asks User if they want to run program
def yes_or_no():
inp = input("Do You want to log every keystoke Y/N?")
if inp == "Y":
print("Running Program")
#Runs Keylog Program
logging.basicConfig(filename=("keylog.txt"), level=logging.DEBUG, format=" %(asctime)s - %(message)s")
elif inp == "N":
print("Ending Program")
sys.exit()
else:
print("You must choose between Y or N")
return yes_or_no()
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener :
listener.join()
#Ends script
yes_or_no()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.