Simple encryption in python

Hi,
i have the below script:

###############################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

Please help me in this.
thanks

The 'break' at the bottom of the loop breaks the loop immediately, preventing j from counting higher.

Bit if I remove the break also I am not getting the intended result

What does it do, then?

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
>>> 

But the output i need is :

120 103 111 112

What algorithm are you trying to do?

in my example: input text is "well".
So, the result should be :

(ascii value for w)+1 (ascii value for e)+2 (ascii value for l)+3  (ascii value for l)+4 
 120 103 111 112

This is what i am trying.
Thanks

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".

tyler_durden

I have tried that:

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 :

120 103 111 112

Thanks

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.

Examples please.

tyler_durden

Please find the example with break:

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?

j=1

is already moved before 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.

tyler_durden

thanks,,it worked.

You are welcome.

tyler_durden