'''create three lists called(letters, Encoded, encryptedLettersNum)''' letters=[] Encoded=[] encryptedLettersNum=[] '''Create a String variables called(EncodedMessage) that will contain message after the encryption''' EncodedMessage='' theAlphabetSmall='a' '''create a for loop that add every element in theAlphabetSmall list to the letters list''' for i in range(0,26): letters.append(theAlphabetSmall) theAlphabetSmall=chr(ord(theAlphabetSmall)+1) '''print a question that tell the user to enter the message and save it in the message variable''' message=input ("Enter your message:\n") '''create a for loop that add every character in the message variable to the letters list and add the index+1 to the encryptionLettersNum list''' for i in range(len (message)): index=letters.index(message[i]) encryptedLettersNum.append(index+1) '''create a for loop in for loop that start from pointer +1 and end in the last character in the messag varible''' for i in range(len (message)): for j in range(i+1,len (message)): ''' create a if condation that see if the index i of the encryptedLettersNum is bigger than the index j of the encryptedLettersNum and if it is not bigger subtract them and set the index i of it to the total ''' if encryptedLettersNum[i]>encryptedLettersNum[j]: encryptedLettersNum[i]=encryptedLettersNum[i]-encryptedLettersNum[j] break '''create a for loop that take the index encryptedLettersNum then decreased by 1 then added to Encoded list''' for i in range(len (message)): Encoded.append(letters[encryptedLettersNum[i]-1]) '''create a for loop that set the encodedMassage variable to the sum of it and the index of the encoded list ''' for i in range(len (message)): EncodedMessage= EncodedMessage+Encoded[i]; '''print the message after encoded with the Encoded message''' print("The message after Encoded:\n" + EncodedMessage)
'''create three lists called(letters, Encoded, encryptedLettersNum)'''
letters=[]
Encoded=[]
encryptedLettersNum=[]
'''Create a String variables called(EncodedMessage) that will contain message
after the encryption'''
EncodedMessage=''
theAlphabetSmall='a'
'''create a for loop that add every element in
theAlphabetSmall list to the letters list'''
for i in range(0,26):
letters.append(theAlphabetSmall)
theAlphabetSmall=chr(ord(theAlphabetSmall)+1)
'''print a question that tell the user to enter the message
and save it in the message variable'''
message=input ("Enter your message:\n")
'''create a for loop that add every character in the message variable
to the letters list and add the index+1 to the encryptionLettersNum list'''
for i in range(len (message)):
index=letters.index(message[i])
encryptedLettersNum.append(index+1)
'''create a for loop in for loop that start from pointer +1
and end in the last character in the messag varible'''
for i in range(len (message)):
for j in range(i+1,len (message)):
''' create a if condation that see if the index i of the encryptedLettersNum
is bigger than the index j of the encryptedLettersNum and if it is not bigger
subtract them and set the index i of it to the total '''
if encryptedLettersNum[i]>encryptedLettersNum[j]:
encryptedLettersNum[i]=encryptedLettersNum[i]-encryptedLettersNum[j]
break
'''create a for loop that take the index encryptedLettersNum
then decreased by 1 then added to Encoded list'''
for i in range(len (message)):
Encoded.append(letters[encryptedLettersNum[i]-1])
'''create a for loop that set the encodedMassage variable to the sum of it and
the index of the encoded list '''
for i in range(len (message)):
EncodedMessage= EncodedMessage+Encoded[i];
'''print the message after encoded with the Encoded message'''
print("The message after Encoded:\n" + EncodedMessage)
-----------------------------------------------------------------
in python 3
i want this piece of code more efficiently in one for loop
for i in range(len (message)):
for j in range(i+1,len (message)):
''' create a if condation that see if the index i of the encryptedLettersNum
is bigger than the index j of the encryptedLettersNum and if it is not bigger
subtract them and set the index i of it to the total '''
if encryptedLettersNum[i]>encryptedLettersNum[j]:
encryptedLettersNum[i]=encryptedLettersNum[i]-encryptedLettersNum[j]
break
-----------------------------------
the output must be like:
Step by step
Solved in 2 steps with 2 images