Python functions and modules (edit: now with more input checking and comments)

I am progressing (albeit slowly) in python, but I seem to have a decent grasp on functions.

In this sample rolldie function, I had an issue with random.randrange. If I specified a range of 6, it would roll 0-5 which is fine. Initially I mistakenly tried to correct for this by adjusting the input provided to random.randrange. A much better solution was to adjust the output of the random.randrange function by doing random.randrange +1.

It was a simple problem with a very simple solution, but it is an important lesson:

If a function is not giving you the output you desire; adjust the function, not the input.

(cue “The More You know” music)

#!C:/python25/
#filename:rollDie.py
#Version 0.5 - Improved error checking of luckyNumber and added comments

import random

def rollDie():
    #DocString
    '''\nUses a random number generator to guess a user specified number.

     Uses two user defined integers (one to define the set, and one as a target number) and then tries to guess at random using random.randrange.\n'''

#rollDie intro
print 'Welcome to the Lucky Number Program!\n'
run = True

while run == True:
    #query for user input and setup variables
    sides = int(raw_input('Enter the number of sides for the dice:'))
    luckyNumber = int(raw_input('Enter your Lucky number:'))
    count = 1
    value = random.randrange(sides) +1

    #Error checking the luckyNumber variable
    while luckyNumber <= 0:
        print 'Impossible to roll', luckyNumber,'!'
        luckyNumber = int(raw_input('Enter your Lucky number:'))
    while luckyNumber > sides:
        print 'You will never roll', luckyNumber, 'with only', sides, 'sides!\n'
        luckyNumber = int(raw_input('Enter your Lucky number:'))

    #OK luckyNumber is good, now to guess it at random
    while value != luckyNumber:
        print value
        value = random.randrange(sides) +1
        count += 1
    print 'Finally got the number ', luckyNumber, '! It only took', count, 'Guesses!'

    #We guessed the lucky number, query for retry
    tryAgain = str (raw_input('Want to try again? Type yes,no or explain\n'))
    while tryAgain == 'explain':
        print rollDie.__doc__
        tryAgain = str (raw_input('Want to try again? Type yes,no or explain\n'))
    if tryAgain == 'no':
        print 'Ok we are done!'
        run = False
    elif tryAgain == 'yes':
        pass
###########################################################################

rollDie()

2 Responses to “Python functions and modules (edit: now with more input checking and comments)”

  1. January 15th, 2008 | 6:31 pm

    Your tabbing is off again ;)

    You’ve got a function definition as if the code is supposed to be in there, but then its all non-indented, meaning it runs the moment the file is parsed, not when the rollDie() is called at the end :)

  2. RawCode
    January 16th, 2008 | 6:01 am

    You are correct! I posted my code snippets over on the Arstechnica forums and luthe was nice enough to point out some errors and makes a few suggestions. I have a rewritten version that I shall post in a bit. And yes, rollDice() will have a body! ;)

    http://episteme.arstechnica.com/eve/forums?a=tpc&s=50009562&f=6330927813&m=358004479831&r=652004189831#652004189831