import random map1={1:"A", 2:"B", 3:"C", 4:"D", 5:"E", 6:"F", 7:"G", 8:"H", 9:"I", 10:"J", 11:"K", 12:"L", 13:"M", 14:"N", 15:"O", 16:"P", 17:"Q", 18:"R", 19:"S", 20:"T", 21:"U", 22:"V", 23:"W", 24:"X", 25:"Y", 26:"Z", 27:"a", 28:"b", 29:"c", 30:"d", 31:"e", 32:"f", 33:"g", 34:"h", 35:"i", 36:"j", 37:"k", 38:"l", 39:"m", 40:"n", 41:"o", 42:"p", 43:"q", 44:"r", 45:"s", 46:"t", 47:"u", 48:"v", 49:"w", 50:"x", 51:"y", 52:"z", 53:"0", 54:"1", 55:"2", 56:"3", 57:"4", 58:"5", 59:"6", 60:"7", 61:"8", 62:"9", 63:"!", 64:"@", 65:"#", 66:"$", 67:"%", 68:"^", 69:"&", 70:"*", 71:"(", 72:")", 73:",", 74:".", 75:"?", 76:":", 77:"'"} map2={"A":1, "B":2, "C":3, "D":4, "E":5, "F":6, "G":7, "H":8, "I":9, "J":10, "K":11, "L":12, "M":13, "N":14, "O":15, "P":16, "Q":17, "R":18, "S":19, "T":20, "U":21, "V":22, "W":23, "X":24, "Y":25, "Z":26, "a":27, "b":28, "c":29, "d":30, "e":31, "f":32, "g":33, "h":34, "i":35, "j":36, "k":37, "l":38, "m":39, "n":40, "o":41, "p":42, "q":43, "r":44, "s":45, "t":46, "u":47, "v":48, "w":49, "x":50, "y":51, "z":52, "0":53, "1":54, "2":55, "3":56, "4":57, "5":58, "6":59, "7":60, "8":61, "9":62, "!":63, "@":64, "#":65, "$":66, "%":67, "^":68, "&":69, "*":70, "(":71, ")":72, ",":73, ".":74, "?":75, ":":76, "'":77} global encode def encode(): key=[] #Here, we get the message that is to be encoded, and transform it into a list in convert #At the same time we record it's number version in convert2. #It is important to know that convert doesn't actually do anything in the long run, its kindof a decoration for reference. print("To begin encoding, please type in the message you want encoded below") plaintext=input(": ") convert=[] convert2=[] for letters in plaintext: if letters!=' ': convert.append(letters) convert2.append(map2[letters]) #print(convert2) elif letters==' ': pass print("Message recieceved successfully. Now we will encode it") print("To encode your message, we will need %s letters as a key" % len(convert)) print("Would you like to enter your own key, or would you like me to get a randomly generated one for you?") print("If you have your own key, type 'Have.' If you need me to generate a random one, type 'Dont.'") #This code below gets the key for encoding. It's kindof complex, becuase you have a choice #Key2 in both scenarios records the letter versions of the key, #While Key3 records the letter versions. while True: key1=input(": ") if key1=="Have": print("Okay, type it in below") key2=input(": ") if len(key2)>len(convert): print("The key has to be %s characters long(no spaces)" % len(convert)) print("If you have your own key, type 'Have.' If you need me to generate a random one, type 'Dont.'") elif len(key2)<len(convert): print("The key has to be %s characters long(no spaces)" % len(convert)) print("If you have your own key, type 'Have.' If you need me to generate a random one, type 'Dont.'") else: global key3 key3=[] for letters in key2: if letters!=' ': key2+=letters key3.append(map2[letters]) #print(key3) break elif key1=="Dont": print("Okay, getting random letters now...") key2=[] key3=[] for letters in key2: if letters!=' ': key3.append(map2[letters]) elif letters==' ': pass for x in range(0,len(convert)): z=random.randrange(0,77) key2.append(map1[z]) key3.append(z) key4=' ' counter=0 for x in range(0,len(convert)): key4+=key2[counter] key4+=' ' counter+=1 #print(key2) #print(key3) print("%s is your key now" % key4) break else: print("Sorry, I didn't get that. Please try again") #Now we're actually encoding the message print("Encoding...") convert3=[] counter=-1 #Here for x in range(0, len(convert)): counter+=1 convert3.append(convert2[counter]+key3[counter]) if convert3[counter]>77: convert3[counter]-=77 #print(convert3) counter=1 for x in range(0,len(convert)): convert3[counter-1]=map1[convert3[counter-1]] counter+=1 final=' ' counter=0 for x in range(0, len(convert)): final+=convert3[counter] final+=' ' counter+=1 print("Your message now reads: %s" % final) print("If you are done with the Schroeder Coder 4.0, type in 'Done'") print("If you want to continue encoding or decoding, type in 'Restart'") while True: y=input(": ") if y=="Restart": redo() break elif y=="Done": pass break else: print("Sorry, I didn't get that. Try again.") def decode(): print("To begin the decoding process, please type in the message that you need decoded.") ciphertext=input(": ") convert=[] convert2=[] for letters in ciphertext: if letters!=' ': convert.append(letters) convert2.append(map2[letters]) else: pass print("Message received.") print("Now we need the key that was used") print("Please type the %s character key in below" % len(convert)) key=input(": ") key2=[] key3=[] for letters in key: if letters!=' ': key2.append(letters) key3.append(map2[letters]) print("Key received successfully") print("Decoding...") convert3=[] counter=-1 for x in range(0, len(convert)): counter+=1 convert3.append(convert2[counter]-key3[counter]) if convert3[counter]<1: convert3[counter]+=77 counter=1 for x in range(0, len(convert)): convert3[counter-1]=map1[convert3[counter-1]] counter+=1 final=' ' counter=0 for x in range(0,len(convert)): final+=convert3[counter] final+=' ' counter+=1 print("The message says: %s" % final) print("If you are done with the Schroeder Coder 4.0, type in 'Done'") print("If you want to continue encoding or decoding, type in 'Restart'") while True: y=input(": ") if y=="Restart": redo() break elif y=="Done": pass break else: print("Sorry, I didn't get that. Try again.") global redo def redo(): print("So, are you encoding a message or decoding one?") print("Please type 'encoding' or 'decoding'") while True: x=input() if x=="encoding": encode() break elif x=="decoding": decode() break else: print("Sorry, I didn't get that.") print("Try again") print("Welcome to the Schroeder Coder 4.0, now with a better user interface") redo()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question