Connecting Tech Pros Worldwide Help | Site Map

Beginner need help!

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 2 '09
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

I know that if I run it now it adds a new letter for 26 lines but i need none of those letters to repeat.
Member
 
Join Date: Nov 2008
Posts: 47
#2: Oct 2 '09

re: Beginner need help!


Hi

One way would be to reduce alphabet as you go along.

(1) So create the random number with e.g. i=random.randrange(len(alphabet)) (this would mean changing the loop to run over range(len(alphabet)) or creating a copy of alphabet)
(2) Then add to your newstring with myNewString+=alphabet[i]
(3) The take that letter out of alphabet with something like:
alphabet=alphabet[:i]+alphabet[i+1:]

Hope that points you in the right direction
Reply