我是一個Python和編程的半小老鼠,但是當我雙擊打開.py文件時,我的程序無法正常工作。當我通過命令行運行它並通過IDE(Pycharm)運行它時。基本上,當雙擊時,它會到達key = input(「Enter decode key:」)部分,輸入內容後會關閉。任何幫助將不勝感激。我知道我的代碼不夠優雅,它只是需要工作。 Python 3.5.2Python腳本無法右鍵雙擊時運行.py
如果有幫助,樣本輸入將是23的「關鍵」和142,128,133,123,134,142「編碼」。它應該輸出「窗口」,並在命令行和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")
您可以放在最後:''input(」按Enter退出「)'' – jasonharper
對不起,我只是改了標題。最初,我認爲它最終不會暫停。但後來我意識到它甚至沒有通過該計劃。它應該已經暫停後打印出信息(我有y =輸入) – zinzara
我不能完全知道發生了什麼。我認爲你的粘貼代碼有一些格式錯誤。 –