[Tutor] Why doesn't it choose a new number each time?

wesley chun wescpy at gmail.com
Wed Feb 14 01:12:48 CET 2007

>  I wrote a rock, paper, scissors game and every time you play without
> exiting, it chooses the same number. How can I fix this problem? The
> relative code is below:
>        :
>         compchoice = choice(range(3))

hey nathan,

neat program... that's one of the homework assignments in my book!  i
love to give this exercise to my students.  while i don't have enough
time to scan your entire program to tell you what the problem is with
your solution, i *can* tell you that the line of code you have there
will let the computer only choose 0, 1, or 2, not 1, 2, or 3.  you
have to use range(1,4) for that, or do the same thing but add 1 to it,
e.g., choice()+1.

as far as how to improve your code, if you study your program
carefully, you should be able to reduce the overall number of print
statements, and then later, when you really become an expert, to
reduce the number of if statements.  in the end, you can refactor your
solution down to, say, 10 lines or less.

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca

More information about the Tutor mailing list