[Tutor] Question about Python.

Alan Gauld alan.gauld at btinternet.com
Wed Mar 25 18:57:16 CET 2009


"T" <suger_cube at hotmail.com> wrote

> I'm working on Paper, Rock, Scissors in Python.
> I need to make it a loop, and test the values 
> (1, 2, 3, /Rock/Paper/Scissors) yet, but i'm kind 
> of stuck. Could you help me?

What exactly puzzles you? You know you need 
a loop so you presumably realize that you need 
to repeat some part of your program.

What bit needs to be repeated? 
Is it the bit that you ask the platyer to choose a value? 
Or where the computer calculates a value? 
Or where you work out the result? Or all of these?

And how many times, or until what condition, should it repeat?
How will you detect that value?

If you answer those questions you might start to see 
what needs to be done?

There are a few commens on the code below:

def main():  #First function.
    print 'Lets play Paper Rock Scissors!\n'
    print '1 For Rock\n2 For Paper\n3 For Scissors\n4 To Quit'
    number=raw_input ('What do you choose? ') #Gets users input.

number sems a bit vague, this is actually the players choice.
So call it PlayersChoice or some such descriptive name.
Even just pc maybe - see below...

    pc = ComputerChoice()

And pc seems an odd name to choose for ComputersChoice()
Why not cc?

    PlayerChoice(number, pc)

And this function doesn't actually return the players choice, 
it displays the result. So maybe DisplayResult wouldbe a 
better name?

def ComputerChoice(): #Compuers function
    ComputerChoice = random.randrange(1, 4) #Computers random range.
    return ComputerChoice

Its not a good idea to use a variable name the same as the function. 
This will prevent you using recursion (which you might not know 
about yet but is important later!) You could simply use 'choice', its 
shorter to type too!

def PlayerChoice(number, CC): #Uses the users input & compares 
    number = int(number)                             #With the computers.

Its probably better to convert the number where you read it from 
the user - that way you can tell them they made a mistake and 
get a better response before you call this function. And the function 
can just expect a number as input.


    print "\n"
    if CC == 1 and number == 3:
        print 'Computer wins: Rock beats Scissors'
    elif CC == 1 and number == 2:
        print 'Player wins: Paper beats Rock'
    elif CC == 2 and number == 3:
        print 'Player wins: Scissors beat paper'
    elif CC == 3 and number == 1:
        print 'Player wins: Rock beats scissors'
    elif CC == 2 and number == 1:
        print 'Computer wins: Paper beats rock'
    elif CC == number:
        print '''Draw!''' #Trying it with 3
    elif CC == 3 and number == 2:
        print 'Computer wins: Scissors beats rock'
    elif number == 4:
        print 'Goodbye'

OK, This is stuff you probably want to take out and put 
beside the code for reading the user input. Its not really 
part of the game. In fact this might be what you use to 
terminate your loop?

    else:
        print CC
        print number
    if number != 4:  
        print '\n'
        main()

Looks like you discovered recursion already, although 
I suspect you don't know it yet? :-)


#Start of program
main()


HTH,


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/l2p/



More information about the Tutor mailing list