简体   繁体   English

我将如何制作它,以便我可以在该程序开始时询问用户是否想要执行它,因为下面的代码会带来语法错误

[英]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.

相关问题 我怎样才能测试下面的代码,以便我可以看到决胜局的运作方式? - How would I be able to test the below code so that I can see how the tiebreaker would function? 我如何制作一个while循环来询问用户是否要重新启动程序 - How do I make a while loop to ask the user if they want to restart the program 如何询问用户是否希望代码重新开始 - how to ask the user if they want the code to start again 我将如何做到(如果Sector [1] ==“ l”和BRCTerritories [1] ==“。” :)调出表中的所有结果? - How would I make it so (if Sector[1]==“l” and BRCTerritories[1]==“.”:) brings up all the results in the table? 如果用户在我的 if 语句后回答“否”,我如何才能让代码再次询问您的姓名? - How do I make it so that the code will ask you your name again if the user answers no after my if statement? 我怎么会问他们是否要重新计算? - How would I ask if they want to calculate again? 如何做到这一点,以便我可以将按钮应用于我想要的任何 window? - How would make it so I can apply a button to any window I want? 我如何让我的程序向用户提出更多问题? - How do i make my program ask further questions to the user? 如果用户在 Python 中输入了错误的答案,我该如何让程序重新提问? - If the User inputs the incorrect answer in Python, how can I make the program ask the question again? 我将如何循环这段代码,以便它重新开始? - How would i loop this bit of code so that It will go to the start again?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM