Dice throwing game variable players and throws

TrustyTony 0 Tallied Votes 779 Views Share

I wrote this as reaction for Pascal newbie's question in Pascal Delphi forum.

import random

# get Python 2 to use Python 3 style input
try:
    input = raw_input
except:
    pass

def number_of_things(thing='turns'):
    while True:
        try:
            return int(input('Give number of %s decided: ' % thing))
        except ValueError:
            print('Please enter integer value')
        
def throws(times=1, players=2):
    return [[random.randint(1,6) for count in range(times)] for playercount in range(players)]

def show_n_players(number_of_players=2):
    game = throws(number_of_things(), number_of_players)
    for number, got in enumerate(game, 1):
        print('Player %i: throws: %s, sum %i' % (number, got, sum(got)))
    print('')
    print(('Winner was player %i' %
           max((sum(got), no)
               for no, got in enumerate(game, 1))[1]).center(
                   60, '*'))

show_n_players(number_of_things('players'))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.