[Tutor] comapring lists

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Nov 29 18:41:54 CET 2004



On Mon, 15 Nov 2004, Jim De Caro wrote:

> I am getting user input and comparing it to an iterated list.  I can get
> the input to print out verbatim.  I want to then "swap" individual
> letters in the input with letters from a second list so it is like a
> simple encryption.  I can't figure out the correct logic or syntax.
> Here is what I have so far:
>
> user = raw_input("Enter your selection: ")

[some code cut]

> for i in range(len(encrypt)):
> 	print user[0:62]
> 	break



Hi Jim,

The 'break' statements here prematurely escape out of the loop.  The code
above,

###
for i in range(len(encrypt)):
    print user[0:62]
    break
###


ends up reducing down to:

###
if len(encrypt) > 0:
    print user[0:62]
###

which is probably not what you want.




> for j in range(len(decrypt)):
>     for j in zip(decrypt):
> 	print 'Encrypted-> %s' % (j,user)
> 	break
>
> This does not work.  Any suggestions?


Try breaking the problem down a bit into a few subproblems.  It looks like
the code is trying to do too much at once --- whenever I see nested loops
like this, I get nervous.  *grin*


In particular, can you define a function that takes a single character,
and encrypts that character alone?


Good luck to you!



[Meta note: You need to be subscribed to tutor to be able to post messages
directly to the list.  Otherwise, your messages get put on a moderation
queue.

Please subscribe at: http://mail.python.org/mailman/listinfo/tutor.]



More information about the Tutor mailing list