开发者

How do I make this only print the last value?

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)
开发者_C百科
        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?


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


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


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)


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


Using a generator:

message= raw_input("Enter message to be encrypted: ")
key=0b1010101
print ''.join(chr(key^ord(c)) for c in message)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜