python双击py一闪_Python脚本在双击.py时无法正常运行

博主遇到一个问题,他的Python程序在双击.py文件时无法正常运行,但通过IDE(PyCharm)从命令行启动则能正确执行。问题在于程序在用户输入关键数据后即关闭。代码中包含了数据解码功能,需要用户输入密钥和已编码数据,然后解码并输出。在双击运行时,程序在等待用户输入key之后就终止了。
摘要由CSDN通过智能技术生成

我在Python和编程方面是个半文盲,但是当我双击打开.py文件时,我的程序就不能正常工作了。当我通过IDE(Pycharm)从命令行运行它时,它就会出现。基本上,当双击时,它将到达key=input(“Enter-decode key:”)部分,键入内容后,它将关闭。任何帮助都将不胜感激。我知道我的代码并不优雅,它只是需要工作。Python 3.5.2

如果有帮助,“key”的示例输入将是23,而“encoded”的示例输入是142128133123134142。它应该输出“window”,并在命令行和Pycharm中成功输出。在import sys

def main(key, encoded):

if encoded == 0: #This should run if program was double-clicked (no arguments)

encoded = input("Paste encoded JavaScript: ")

key = input("Enter decode key: ")

def decode(key, encoded): #Decode the data

encoded = encoded.split(',') #Split each section delimiting by a colon

decoded = []

for x in encoded:

x = int(x) - int(key) #Subtract the key from the number in each section

decoded.append(chr(x)) #Change from ASCII decimal code to the ASCII character

decoded = ''.join(decoded) #Join back into a string

print(".")

print(".")

print(".")

print(".")

print("Encoded data:")

print(encoded)

print("Decode key:")

print(key)

print("Decoded data:")

print(decoded)

return 0

decode(key, encoded) #Jump into the decode function

return 0

if __name__ == "__main__":

try:

if len(sys.argv) > 1: #If length is greater than 1, then there were arguments added upon program execution

key = sys.argv[1] #The "key" should be the first argument

encoded = sys.argv[2] #The "encoded" data should follow

else:

key = 0 #If length is anything else, then set them to 0 and ask for the data later

encoded = 0

main(key, encoded) #Jump into main function and pass the key and encoded arguments

finally:

input("Press Enter to exit")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值