My first Python Script_Generate a certain type string of a certain length

11 views
Skip to first unread message

malinda2008

unread,
Dec 21, 2012, 8:07:38 AM12/21/12
to qda_...@googlegroups.com
To accomplish teacher Hong's homework,I wrote a piece of code this afternoon.There are no use guide or UI yet.Just look at the script and help me find defects.Or you can suggest better algorithm.Welcome!


CapLetters=['A','B','C','D','E']
SmaLetters=['a','b','c','d','e']
Numbers=['1','2','3','4','5']
Specials=['!','@','#','$','%']
Direction={'U':CapLetters,'L':SmaLetters,'N':Numbers,'S':Specials}

import random

def GenRandStr(strlen,*shortletter):
        
    BeforeSort=[]
    TypeNum=len(shortletter)
    
    if TypeNum>strlen:
        print "Wrong!String too short!"
        return ""
    else:
        for EachLetter in shortletter:
                if Direction.has_key(EachLetter):
                    ExtendList=Direction[EachLetter]
                    BeforeSort.extend(random.choice(ExtendList))
                else:
                    print "Unexpected parameters!Try again!"
                    return ""

        Joined=JoinLists(shortletter)
        for i in range(0,strlen-TypeNum):
            BeforeSort.extend(random.choice(Joined))
        random.shuffle(BeforeSort)
        JoinString=""
        return JoinString.join(BeforeSort)

def JoinLists(somelist):
    JoinedList=[]
    for Type in somelist:
        JoinedList.extend(Direction[Type])
    return JoinedList

print GenRandStr(5,"U","L","N")
#raw_input('Press enter to exit.')

Arthur

unread,
Dec 21, 2012, 8:37:54 AM12/21/12
to qda_...@googlegroups.com
Excellent job, Man!!

Here is a tip for you. Next time when you paste your code in the topic, you can use the Highlight Code Syntax function to make your code more readable, just look like this.. :-)

Chris

unread,
Dec 21, 2012, 9:29:49 AM12/21/12
to malinda2008, qda_...@googlegroups.com
You are the best I have ever seen. Excellent girl !!!

You are so intelligent,following your deep heart,and achieve it. you are my idol. 

 Just like the first strong impress you give me. when Ath said some new tech.,you always can explain it using your own logic.

I hope you can keep this enthusiasm.  keep going........


We are forever young.


2012/12/21 malinda2008 <malin...@gmail.com>

--
 
 



--



------------------------------------------
-----  Excellent is a habit. -----
------------------------------------------

张明旭张

unread,
Dec 23, 2012, 10:24:09 PM12/23/12
to Arthur, qda_...@googlegroups.com
Thanks, my mentor. Really helpful suggest.


2012/12/21 Arthur <hfan...@gmail.com>
--
 
 

张明旭张

unread,
Dec 23, 2012, 10:26:14 PM12/23/12
to Chris, qda_...@googlegroups.com
Thanks,Chris! You are my idol too. Next stop:a lesson of "class" and a dictionary of my own.


2012/12/21 Chris <iloo...@gmail.com>
Reply all
Reply to author
Forward
0 new messages