{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.5 Case Study: A Game of Chance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We simulate the game of \"craps\". The player repeately rolls two dice and adds the faces that come up.\n", "- #### On the first roll:\n", " - #### If the sum is 7 , the player immediately wins.\n", " - #### If the sum is 2, 3, or 12, the player immediately loses.\n", " - #### Any other sum becomes the player's \"point\" and the game continues.\n", "- #### On each subsequent roll:\n", " - #### If the player \"makes the point\" (rolls the point value), the player wins.\n", " - #### If the player's sum is 7, the player loses and the game ends.\n", " - #### Otherwise, the game continues." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def roll_dice():\n", " \"\"\"Roll two dice and return their face values as a tuple.\"\"\"\n", " die1 = random.randrange(1, 7)\n", " die2 = random.randrange(1, 7)\n", " return (die1, die2) # pack die face values into a tuple" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def display_dice(dice):\n", " \"\"\"Display one roll of the two dice.\"\"\"\n", " die1, die2 = dice # unpack the tuple into variables die1 and die2\n", " print(f'Player rolled {die1} + {die2} = {sum(dice)}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### First roll." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "die_values = roll_dice()\n", "display_dice(die_values)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Determine the game status and the player's point, based on first roll." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum_of_dice = sum(die_values)\n", "\n", "if sum_of_dice in (7, 11): # win\n", " game_status = 'WON'\n", "elif sum_of_dice in (2, 3, 12): # lose\n", " game_status = 'LOST'\n", " \n", "else: # remember point\n", " game_status = 'CONTINUE'\n", " my_point = sum_of_dice\n", " print('Point is', my_point)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Continue rolling until player wins or loses." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "while game_status == 'CONTINUE':\n", " die_values = roll_dice()\n", " display_dice(die_values)\n", " sum_of_dice = sum(die_values)\n", "\n", " if sum_of_dice == my_point: # win by making point\n", " game_status = 'WON'\n", " elif sum_of_dice == 7: # lose by rolling 7\n", " game_status = 'LOST'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Display the \"wins\" or \"loses\" message." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if game_status == 'WON':\n", " print('Player wins')\n", "else:\n", " print('Player loses')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXAMPLE:** Pack a `student` tuple with the name 'Sue' and the list `[89, 94, 85]` and display the tuple. Then unpack the tuple into variables `name` and `grades`, and display their values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "student = ('Sue', [89, 94, 85])\n", "student" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name, grades = student" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grades" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2023 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }