Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

Working on problem set #5, problem 2. As part of the assignment they provide us with a function called deal_hand(n) that generates a random set of letters of specified length n; i have copied this code to codepad. Code is straight from assignment, I added the print statements. http://codepad.org/Ryzo8Tx5. What I find weird is the discrepancy b/w the random number generated, the value of x, and the value of the right side of the equation of x. For example, if the random number = 1, then x = VOWELS[4] = e, right?? In the first loop in codepad it shows random number = 1, x=o, and VOWEL[2]=a

OpenStudy (anonymous):

Perhaps I just don't understand the randrange function. Any help is appreciated. Thanks!

OpenStudy (anonymous):

updated the code to match variables better to my message: http://codepad.org/dhIN95J4

OpenStudy (anonymous):

I'm with you, though when I created my own file...it works as we would suspect. (I tried Codepad..but it keeps returning me an internal error, forgive me) import random VOWELS = 'aeiou' for i in range(0, 5): x = random.randrange(0, len(VOWELS)) print x print i,'=i','.................', x, '=', VOWELS[x] >>> ================================ RESTART ================================ >>> 0 0 =i ................. 0 = a 4 1 =i ................. 4 = u 1 2 =i ................. 1 = e 4 3 =i ................. 4 = u 1 4 =i ................. 1 = e random.randrange(0,N) simply randomly selects a number between 0 and N. the VOWELS_randNum in the original script baffles me as well.

OpenStudy (anonymous):

http://codepad.org/XvtPuo89

OpenStudy (anonymous):

@dvena - your print statements at lines 24 and 26 each call randrange which produce a different number than line 23 --> you are comparing apples to oranges http://codepad.org/QcWj94eK

OpenStudy (anonymous):

@bray - yea it worked fine, but I was just curious as to what I was missing and why it didn't seem to make sense. @bwCA - it all makes sense now! Thanks!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!