[Tutor] Help!

Tim Bowden tim.bowden at mapforge.com.au
Wed Sep 30 08:03:21 CEST 2009


On Tue, 2009-09-29 at 20:38 -0700, Jessica Poveda wrote:
> I need help writting a program.
> 1) Random string generation 
> 2) no repeating letters 
> Can anyone help me,please? I am so confused. The only problem is I
> have to use the code that is written there but add on to it.  
>  
> import random
>  
> alphabet = "abcdefghijklmnopqrstuvwxyz"
> myNewString = ""
> for letters in alphabet:
>     myNewString = myNewString + alphabet[random.randrange(26)]
>     print myNewString 
> 

Couple of points:
You're using an iterable sequence type object (string: myNewString) for
looping.  That's a good habit; much better than using an array index or
such which you might get in the habit of doing in some other languages
(c, java...), though we might be able to find a better approach to this
problem.

Your print statement is inside your 'for' loop.  It's printing the
myNewString every time you add a new element.  If all you want to do is
print it at the end, then put it after the loop; if it's at the same
level of indentation as the loop, then it's part of the loop.

If you don't want any letter repeated then you will want to take it out
once it's used.  There are two problems here.  Firstly, you don't want
to go mucking about changing a sequence type object while you're
iterating over it unless you really know what you're doing; it can get
ugly.

Secondly, while a string is iterable, it's immutable; in other words you
can't change it.  That means we need to find some sort of iterable
sequence type of object that is not immutable; ie, we can change it (by
taking used letters out of it).  So, what sort of iterable sequences are
there?  You know about strings, but have a look at lists
['a','b','c',...] and tuples ('a','b','c'...).  One of those constructs
is mutable and should fit the bill.

Once you've found an iterable sequence that is mutable (I've only given
you two possibilities to look at) you then need to solve the problem of
how to loop if you're going to be taking a letter out of your list of
letters every time (oops!).  Here's where you need to recognise a binary
condition.  You could say "I want to loop 26 times", and that would be
correct.  But you could also say "I want to loop while I've got unused
letters left".  Expressed like that, you can see a binary condition
(I've either got unused letters left or I don't; only two
possibilities).

Recognising such a binary condition is a very valuable skill.  It takes
a bit of practice, but it's a very common occurrence. Every time you do
a test (if, while...) you're doing a binary test that evaluates to
either true of false.  In this case, instead of looping over your
iterable sequence (such as a string, list or tuple as appropriate) why
not try doing another iteration if there are still letters left in your
sequence; Ie:
(pseudo code):

while (myLetterSquence is not empty):
  get a letter at random from myLetterSequence
  add the random letter to myRandomLetterString
  remove the same random letter from myLetterSequence
print myRandomLetterString

So how do we tell if an iterable sequence has any elements left?  An
empty sequence will evaluate to false, a sequence that is not empty will
evaluate to true.  Ie:

myList = ['a','b']
if (myList):
  print "myList evaluated to true!"

myEmptyList = []
if (myEmptyList):
  print "This won't print!"
else:
  print "myEmptyList evaluated to False!"


Short note:  Constructs like myVar = myVar + 1 are very common.  You
will see that the myVar gets repeated on both sides of the '='.  Lets
have a closer look at what's going on here;  First, 'myVar + 1' gets
evaluated, then myVar = (result of previous evaluation) gets evaluated.
Given 'myVar' is in both evaluations, smart programmers get lazy (lazy
like a fox, not sloth like lazy) and decide not to repeat the variable
name.  The result? 'myVar += 1' meaning do the '+' operation first, then
the '=' operation.  It can be a bit confusing at first so feel free not
to use it when you're first starting out, but it happens often enough
that you'll come across it regularly.  Just mentally substitute it for
'myVar = myVar + 1' till it becomes second nature.

HTH,
Tim Bowden




More information about the Tutor mailing list