The part I'm really wondering about is in the "if, elif, else" part. I set the variable to bring up the raw input thing so it would stay there long enough to read instead of just printing a response and closing the program right away. There must be a better way of doing this. An answer about that, and any other tips or suggestions would be greatly appreciated. This is my first program.

import random

#Define rolling function.
def rollDice():

#Set min and max levels for random.
    min = 1
    max = 20

#Set variable to yes, then set up 'while' loop.
    rollAgain = 'yes'
    while rollAgain == 'yes' or rollAgain == 'y':
        print ('Sam rolled ' + str(random.randrange(min,max)) + '.')
        print ('Jeff rolled ' + str(random.randrange(min,max)) + '.')

#Variable value is reset by user - loops to begining if answer is 'yes.'
        rollAgain = raw_input('Would you like to roll again?')

#For looks.
        print ('------')

    '''If variable is set to 'no' variable is reset
    as raw_input so text response won't disappear.'''
    if rollAgain == 'no' or rollAgain == 'n':
        rollAgain == raw_input("Okay, I'm going home.")

#First lines read, preface the function.
yN = raw_input('You want to roll?')
if yN == 'yes' or yN == 'y':
    print '------'
elif yN == 'no' or yN == 'n':
    yN == raw_input('Then why am I here?')
    yN == raw_input("I can't understand your sausage fingers, restart the program.")

2 Answers 2


The program could benefit from better separation of concerns. Let the die-rolling function do one pair of rolls, and consolidate all of the looping logic in the main loop. The only tricky bit is that you want the prompts for the first time to be different than for subsequent rolls, so I keep track of the number of rolls.

from itertools import count
from random import randrange

def roll_dice(min=1, max=20):
    print 'Sam rolled %d.' % randrange(min, max)
    print 'Jeff rolled %d.' % randrange(min, max)

for roll_count in count():
    yn = raw_input("Would you like to roll again? " if roll_count else
                   "You want to roll? ")
    if yn in ('yes', 'y'):
        if roll_count:
            print '------'
    elif yn in ('no', 'n'):
        raw_input("Okay, I'm going home." if roll_count else
                  "Then why am I here?")
    elif roll_count == 0:
        raw_input("I can't understand your sausage fingers, restart the program.")

Other minor notes:

  • rollDice should be roll_dice, according to Python naming conventions.
  • Establishing the min and max values as default parameters is not only tidier, it also gives you some flexibility to override the limits.
  • String interpolation is a bit easier to read than "string" + str(something) + "string".

You should make a variable list.

You should make the program into a while loop, then transform it into a for loop. After that, transform it into elifs.

That would make it easy to read and the program a little faster.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.