[Tutor] Random module.. or? CORRECTION

Alan Gauld alan.gauld at btinternet.com
Tue Aug 7 23:42:04 CEST 2007


"bhaaluu" <bhaaluu at gmail.com> wrote

> source code has been written by a Noob, so
> use it at your own risk. =)

Just a few comments...

> def randy():
>  a=[]
>  for i in range(1,10001):
>    a.append(i)

a = range(1,10001)  # range returns a list...

>  b = random.choice(a)
>  print b
>  if b <= 1:

Can never be less than 1 so == would be more 
appropriate. In fact, more accurate too, since 
only one value out of 10000 can give 45. 

>    print "45 points scored!"
>  elif b > 1 and b <= 11:
>    print "30 points scored!"

But here you need to get a value between 30 and 45 so 
another call to choice would be appropriate. Otherwise 
players can only score a very limited set of scores!

>  else:
>    c=[]
>    for i in range(26):
>      c.append(i)

c = range(26)

>    d = random.choice(c)

d = random.choice(range(26))

replaces all of it!

>    print d,"points scored!"

And for consistency I'd make the print statements above 
into assignments to d and then this final print statement 
can be dedented and covers all cases.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list