patternpythonMinor
First dice roller
Viewed 0 times
rollerfirstdice
Problem
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 '------'
rollDice()
elif yN == 'no' or yN == 'n':
yN == raw_input('Then why am I here?')
else:
yN == raw_input("I can't understand your sausage fingers, restart the program.")Solution
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.
Other minor notes:
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 '------'
roll_dice()
continue
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.")
breakOther minor notes:
rollDiceshould beroll_dice, according to Python naming conventions.
- Establishing the
minandmaxvalues 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".
Code Snippets
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 '------'
roll_dice()
continue
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.")
breakContext
StackExchange Code Review Q#90124, answer score: 4
Revisions (0)
No revisions yet.