Create your own
teaching resources

Sign up for a free account to try! It takes less than 20 seconds

Teacherbot 21 Jan, 02:59:22

import random

Set up the main title and number of players

print(“Welcome to Yahtzee!”) num_players = int(input(“How many players are playing? “))

Set up each player

players=[] for player_num in range(num_players): player_name = input(“\nWhat is the name of the player “ + str(player_num+1)+ “? “) players.append(player_name)

Set up the categories

categories = [“Ones”,”Twos”,”Threes”,”Fours”,”Fives”,”Sixes”,”Three-of-a-kind”,”Four-of-a-kind”,”Full House”,”Small Straight”,”Large Straight”,”Yahtzee”,”Chance”]

Create a dictionary to store each players scores

scores={} for player in players: scores[player]={}

Main Loop

rounds = 1 while rounds <= 13: print(“\nRound “ + str(rounds) + “: “+ categories[rounds-1] +” \n”) for player in players: #Generate 5 random dice rolls dice = [] print(“\nRolling for “+player) for die in range(5): roll = random.randint(1,6) print(“You rolled a “ + str(roll)) dice.append(roll) #Print out the list of dice rolls print(player +”’s dice rolls: “ + str(dice))

    #Calculate the score
    if categories[rounds-1] == "Ones":
        score = dice.count(1)

    elif categories[rounds-1] == "Twos":
        score = dice.count(2) * 2

    elif categories[rounds-1] == "Threes":
        score = dice.count(3) *3

    elif categories[rounds-1] == "Fours":
        score = dice.count(4) * 4

    elif categories[rounds-1] == "Fives":
        score = dice.count(5) * 5

    elif categories[rounds-1] == "Sixes":
        score = dice.count(6) * 6

    elif categories[rounds-1] == "Three-of-a-kind":
        if dice.count(1) == 3 or dice.count(2) == 3 or dice.count(3) == 3 or \
           dice.count(4) == 3 or dice.count(5) == 3 or dice.count(6) == 3:
            score = sum(dice)
        else:
            score = 0

    elif categories[rounds-1] == "Four-of-a-kind":
        if dice.count(1) == 4 or dice.count(2) == 4 or dice.count(3) == 4 or \
            dice.count(4) == 4 or dice.count(5) == 4 or dice.count(6) == 4:
            score = sum(dice)
        else:
            score = 0

    elif categories[rounds-1] == "Full House":
        if dice.count(1) == 3 and (dice.count(2) == 2 or dice.count(3) == 2 or \
        dice.count(4) == 2 or dice.count(5) == 2 or dice.count(6) == 2):
            score = 25
        elif dice.count(2) == 3 and (dice.count(1) == 2 or dice.count(3) == 2 or \
        dice.count(4) == 2 or dice.count(5) == 2 or dice.count(6) == 2):
            score = 25
        elif dice.count(3) == 3 and (dice.count(2) == 2 or dice.count(1) == 2 or \
        dice.count(4) == 2 or dice.count(5) == 2 or dice.count(6) == 2):
            score = 25
        elif dice.count(4) == 3 and (dice.count(2) == 2 or dice.count(3) == 2 or \
        dice.count(1) == 2 or dice.count(5) == 2 or dice.count(6) == 2):
            score = 25
        elif dice.count(5) == 3 and (dice.count(2) == 2 or dice.count(3) == 2 or \
        dice.count(4) == 2 or dice.count(1) == 2 or dice.count(6) == 2):
            score = 25
        elif dice.count(6) == 3 and (dice.count(2) == 2 or dice.count(3) == 2 or \
        dice.count(4) == 2 or dice.count(5) == 2 or dice.count(1) == 2):
            score = 25
        else:
            score = 0

    elif categories[rounds-1] == "Small Straight":
        if (1 in dice and 2 in dice and 3 in dice and 4 in dice) or \
           (2 in dice and 3 in dice and 4 in dice and 5 in dice) or \
           (3 in dice and 4 in dice and 5 in dice and 6 in dice):
            score = 30
        else:
            score=0
            
    elif categories[rounds-1] == "Large Straight":
        if (1 in dice and 2 in dice and 3 in dice and 4 in dice and 5 in dice) or \
           (2 in dice and 3 in dice and 4 in dice and 5 in dice and 6 in dice