###############################SimpleEncryption################################
#Simple encryption implemented in python
#Author:pandeeswaran
###############################################################################
def Encrypt(input):
res=''
for i in input:
k = len(input) + 1
j=1
while j < k:
print("j is ",j)
val = ord(i) + j
res = res + ' ' + str(val)
j = j + 1
break
return res
input=str(input("Enter the input string:\n"))
result=Encrypt(input)
print("The encrypted value is\n",result)
While running:
Enter the input string:
well
j is 1
j is 1
j is 1
j is 1
The encrypted value is
120 102 109 109
But the output i expect is :
j is 1
j is 2
j is 3
j is 4
The encrypted value is
120 103 111 112
if i remove the break in the script, i am getting:
Enter the input string:
well
j is 1
j is 2
j is 3
j is 4
j is 1
j is 2
j is 3
j is 4
j is 1
j is 2
j is 3
j is 4
j is 1
j is 2
j is 3
j is 4
The encrypted value is
120 121 122 123 102 103 104 105 109 110 111 112 109 110 111 112
>>>
Looks like your variable "j" is being reset in every loop iteration.
If you want it incremented in every iteration, then move it outside the "for" loop - either at the beginning of the function or immediately below the assignment of "res".
def Encrypt(input):
res=''
j=1
for i in input:
k = len(input) + 1
while j < k:
print("j is ",j)
val = ord(i) + j
res = res + ' ' + str(val)
j = j + 1
return res
input=str(input("Enter the input string:\n"))
result=Encrypt(input)
print("The encrypted value is\n",result)
The result is:
Enter the input string:
well
j is 1
j is 2
j is 3
j is 4
The encrypted value is
120 121 122 123
It's not coming out from the while loop for each iteration.
Even if i use break also, i am getting the same result.,
But my expected result is :
No, you have not. You did move the assignment statement outside the loop, but you also removed the "break" statement.
I did not mention anything about the "break" statement.
def Encrypt(input):
res=''
j=1
for i in input:
k = len(input) + 1
while j < k:
print("j is ",j)
val = ord(i) + j
res = res + ' ' + str(val)
j = j + 1
break
return res
input=str(input("Enter the input string:\n"))
result=Encrypt(input)
print("The encrypted value is\n",result)
The result is:
Enter the input string:
well
j is 1
j is 2
j is 3
j is 4
The encrypted value is
120 121 122 123
Thanks
Which assignment you want to move outside of for loop?
Nope, that's not your original program. Go through the program you posted in your first post carefully and ensure that it is identical to the one above, except for the assignment statement i.e. j = 1.
(Hint: check the indentation of each line.)
Yes I know. That was mentioned in my first post itself.