Beginner need help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carter09
    New Member
    • Oct 2009
    • 1

    Beginner need help!

    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 = "abcdefghijklmn opqrstuvwxyz"
    myNewString = ""
    for letters in alphabet:
    myNewString = myNewString + alphabet[random.randrang e(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.
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Hi

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

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

    Hope that points you in the right direction

    Comment

    Working...