[Tutor] Simple Python Program

Steven D'Aprano steve at pearwood.info
Sun Aug 1 02:55:46 CEST 2010


Hi Jason, 

Let's see if we can clean up some of your program. You had this 
function:

> def inputNames(playerOne, PlayerTwo):
>     p1name = raw_input("Enter your name.")
>     p2name = raw_input("Enter your name.")
>     return playerOne, playerTwo

Let's write this in plain English.

(1) The function takes as input a value called "playerOne", and a value 
called "PlayerTwo". Take careful note of the case (upper and lower) of 
the names, because that is important to Python.

(2) It asks the user for a name, and assigns it to the 
variable "p1name".

(3) It asks the user for a second name, and assigns it to "p2name".

(4) It then ignores p1name and p2name, and does nothing with them.

(5) And returns the variable playerOne and playerTwo. Take careful note 
that the *second* one does not match in case: it starts with a 
lowercase p, so you will get an error because "playerTwo" doesn't 
exist.


Let's re-write it to work. For starters, there's no need to pass the 
players' names to the function which is supposed to get the players' 
names. Secondly, "inputNames" is such a dry, overly technical name. 
Let's make it simpler and friendlier:

def get_names():
    playerOne = raw_input("Enter the first player's name: ")
    playerTwo = raw_input("Enter the second player's name: ")
    return playerOne, playerTwo


Now let's write a function to roll a single dice. Single die. Whatever.

import random  # We need Python's random number generator.

def dice(sides=6):
    return random.randint(1, sides)

This uses a *default value* for the number of sides. If you want to roll 
a ten-sided die, you call:

dice(10)

and it will give back a random number between 1 and 10. But if you want 
to roll the default six-sided die, you can leave out the 6 and just 
write:

dice()

and it will give back a random number between 1 and 10.

Let's write a small program to play dice between two players. We'll make 
a simple dice game function that returns the name of the winning 
player, or the empty string in the case of a draw.

def dice_game(playerOne, playerTwo):
     # playerOne rolls a single six-sided die.
    rollOne = dice()
     # So does playerTwo.
    rollTwo = dice()
    # The winner is the one who gets a higher number.
    if rollOne > rollTwo:
        return playerOne
    elif rollTwo > rollOne:
        return playerTwo
    else:
        # They must have rolled equal, so a draw.
        return ""


Putting it all together, copy the lines between the ###### into a new 
file and save it as a Python program:

######

import random

def dice(sides=6):
    return random.randint(1, sides)

def get_names():
    playerOne = raw_input("Enter the first player's name: ")
    playerTwo = raw_input("Enter the second player's name: ")
    return playerOne, playerTwo

def dice_game(playerOne, playerTwo):
     # playerOne rolls a single six-sided die.
    rollOne = dice()
     # So does playerTwo.
    rollTwo = dice()
    # The winner is the one who gets a higher number.
    if rollOne > rollTwo:
        return playerOne
    elif rollTwo > rollOne:
        return playerTwo
    else:
        # They must have rolled equal, so a draw.
        return ""

def displayWinner(winner):
    print "And the winner is: ",
    if winner == "":
        print "... the game is a draw!"
    else:
        print winner
    print


def main():
    playerOne, playerTwo = get_names()
    play_again = True
    while play_again:
        print "Rolling dice..."
        winner = dice_game(playerOne, playerTwo)
        displayWinner(winner)
        answer = raw_input("Would you like to play again? (y/n) ")
        play_again = answer.strip().lower() in ('y', 'yes')

main()

######


Have a play around with that, and feel free to ask any questions you 
like.


-- 
Steven D'Aprano


More information about the Tutor mailing list