tags:

views:

77

answers:

4
+1  Q: 

for loop python

I have the fallowing code to encrypt a massage:

massage= raw_input("Enter message to be encrypted: ")

spec = chr(0b1010101)

key = ord(spec)

encrypt = ""

for i in range(0, len(massage)):

        encrypt = encrypt + chr(ord(massage[i]) ^ key)

        print encrypt

say I give "yo yo" to it

it will give me :

,

,:

,:u

,:u,

,:u,:

I only need the final answer which is the ,:u,:

what do i have to do?

+2  A: 

Put the print statement outside the loop.

Since the print statement is inside, it is running once per iteration. If it is outside, then it will only do it one time-- once it has finished.

for i in range(0, len(massage)):
    encrypt = encrypt + chr(ord(massage[i]) ^ key)

print encrypt
orangeoctopus
it works.. thanx alot
babikar
if it worked you can accept the message by clicking the checkmark to the left of the answer
Wayne Werner
it says that i have to wait for 5 min in order for me to accept the message!
babikar
A: 

unindent the call to print. This will take it out of the for loop and only print its value when the loop is finished.

On a slightly different note, you might want to work on your acceptance rate if you want people to put time and effort into answering your questions. You've asked 8 questions so far and you haven't accepted an answer to any of them. (Click the arrow next to an answer to accept it)

Josiah
A: 
message= raw_input("Enter message to be encrypted: ")

spec = chr(0b1010101)

key = ord(spec)

encrypt = ""

for i in range(0, len(message)):

    encrypt = encrypt + chr(ord(message[i]) ^ key)

print encrypt
Alex Bliskovsky
A: 

Move the print statement outside the for loop. To do that you need to unindent the print statement.

Malcolm Post