[英]Python Script won't run right when double-clicking .py
我是Python和编程的半菜鸟,但是当我双击打开.py文件时,我的程序将无法正常工作。 当我从命令行运行它时,它将通过IDE(Pycharm)。 基本上,当双击时,它将到达key = input(“输入解码键:”)部分,在输入内容后,它将关闭。 任何帮助将不胜感激。 我知道我的代码不优雅,只需要工作。 Python 3.5.2
如果有帮助,“密钥”的样本输入为23,“编码”的样本输入为142,128,133,123,134,142。 它应该输出“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")
很容易在代码的末尾添加一个输入(''),以便脚本粘在那里
我建议用异常处理来包装你的程序。
以下是代码最后一部分的代码:
if __name__ == "__main__":
try:
if len(sys.argv) > 1:
# ...
finally:
input("Press the enter to exit")
通过这样做,无论您的程序成功完成还是失败,您都会收到提示,要求您按Enter键。 这将使您有机会在关闭窗口之前阅读屏幕上的内容。
只是在程序结束时input("")
一个input("")
将无法在程序失败的情况下运行,您可以在此处指出。
我想我刚刚解决了我的问题。 出于某种原因,即使我的路径设置为Python 3.5并且我右键单击以3.5打开它并且它只是在2.7中打开,它仍然在我的计算机上使用Python 2.7时双击.py ...所以,这只会打开一个新问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.