CSC 221: Introduction to Programming
Fall 2023

HW 3: Python Functions

This assignment will involve writing several Python functions. You should save all of your functions in a single Python module named lastnameHW3 (where lastname is your last name). The module should have a comment block at the top, containing the file name, your name, the date, and a brief description. Each function should also have a doc string that describes its behavior. This work must be entirely your own, with no outside assistance other than the instructor.

Part 1: Repackaged Time

In HW2, you wrote code that calculated observed and relative time for a traveler. The first part of this assignment will involve repackaging your code into functions, as well as writing a new function to construct a table of times.

  1. Define a function named observedTime that encapsulates the calculation of observed time from HW2. This function should take two inputs, a distance (in light years) and a velocity (in percentage of light speed), and return the observed time for traveling that distance at that speed. For example, observedTime(4.2, 50) would return 8.4.
  2. Similarly, define a function named relativeTime that encapsulates the relative time calculation from HW2. This function should take two inputs, a distance (in light years) and a velocity (in percentage of light speed), and return the relative time for traveling that distance at that speed. For example, relativeTime(4.2, 50) would return 7.2746...
  3. Finally, define a function named timeTable that takes one input, a distance (in light years), and prints a table of the observed and relative times for traveling that distance at increments of 5%. Note that it must call your observedTime and relativeTime functions to calculate the travel times. The times should appear in columns with headings, and should be rounded to the nearest tenth of a year. For example, timeTable(4.2) would produce the following output:

    There are complex ways to format output so that columns align, and you may research those if you wish. For this assignment, it is sufficient to output tab characters ("\t") to align the columns (assuming reasonable distances).

Part 2: Game Simulations

Consider a simple game called faceOff involving two players and dice. In each round of the game, Player1 rolls two 9-sided dice while Player2 rolls three 6-sided dice. Whichever player has the higher total wins that round. You might observe that Player2 has a slight advantage since their smallest possible roll is 3 while it is possible for Player1 to roll 2. To offset this advantage, any ties (i.e., both players roll the same total) will count as wins for Player1.

The following function simulates a faceOff game, displaying the totals for each player on each round.

def faceOff(numRounds:int = 10) -> None: for rep in range(numRounds): total1 = casino.rollDie(9) + casino.rollDie(9) total2 = casino.rollDie(6) + casino.rollDie(6) + casino.rollDie(6) print(total1, "-", total2)
  1. Modify the faceOff function so that instead of displaying the roll totals for each round, it compares those totals and keeps track of the number of rounds won by each player. After all the rounds have been completed, it compares the rounds won by each player and returns a string identifying the winner. That is, if Player1 won more rounds than Player2, it returns "Player1". If Player2 won more rounds, it returns "Player2". If they won the same number of rounds, it returns "Tie".
  2. Once you have thoroughly tested your faceOff function, define a function named faceOffStats that has two inputs, a number of games to be simulated and the number of rounds per game. It should simulate games of faceOff (by calling your faceOff function) and keep track of the number of wins by each player. It should display the number and percentage of wins (rounded to one decimal place) for each player. The output should look like the following: Out of 1000 games of 100 rounds: Player1 wins = 468 (46.8%) Player2 wins = 449 (44.9%)