## CSC 221: Introduction to Programming Fall 2018

### HW 2: Python Modules

This assignment will involve writing three small Python modules. These modules are similar to the examples from class (population estimation and reading level) in that they prompt the user for input values, perform calculations on those values, and display the results in a message (or a turtle graphics window). Your modules should follow the same basic style as the examples from class, with a comment block at the top documenting the purpose of the module, meaningful variable names, and readable I/O messages.

## 1. Play Ball

In the past two decades, professional baseball has been revolutionized by the use of statistics to analyze player and team performance. The driving force behind this movement was Bill James, who introduced many of the stats used today (e.g., runs created, win share, range factor). Perhaps his most famous innovation is the Pythagorean Winning Percentage, which predicts the winning percentage of a team based on how many runs they score (RS) and how many they allow (RA):

You are to write a Python module, named lastnameBall.py (where lastname is your last name). When executed, your module should prompt the user for the number of runs scored and runs allowed for a team, then calculate the expected winning percentage using James' formula. It should display this percentage, along with the projected record for the team over a 162-game season. For example:

Enter the number of runs scored: 649 Enter the number of runs allowed: 577 The expected winning percentage is 0.5585257183774681 Over a full season, that projects to 90-72

Hint: Wins and losses should be rounded to the nearest integer. The built-in round function should prove useful here. For example, round(2.4) evaluates to 2, while round(2.6) evaluates to 3.

Hint: When you display multiple values in a print statement, the values are separated by a space. If you want to print values without intervening spaces, you will need to convert them to strings and then concatenate those strings. For example, print(1, 2) will print 1 2, while print(str(1)+str(2)) will print 12.

Hint: To produce a blank line in the output, simply call print without any inputs: print().

## 2. Wibbley Wobbley Timey Wimey

One of the consequences of Einstein's Theory of Relativity is the phenomenon of time dilation. As a traveler's velocity increases, perceived time actually slows for that traveler (relative to an outside observer). For everyday velocities, e.g., riding in a car or airplane, the effect is negligible. However, if spacecraft are eventually invented whose velocities approach the speed of light, the difference in relative time would be dramatic. For example, at 50% of light speed, it would take 8.4 years for a ship to reach Proxima Centauri (as observed from earth), but it would only seem like 3.6 years on board the ship. The factor by which relative time is determined is listed below, where V is the velocity as a percentage of light speed:

STEP 1: You are to write a Python module, named lastnameTime.py (where lastname is your last name). When executed, your module should prompt the user for a distance (in light years) and a velocity (as a percentage of the speed of light), then calculate and print the observed and relative times it would take to reach that distance. The times should be rounded to the nearest tenth. For example:

Enter a distance (in light years): 4.2 Enter the velocity (% of light speed): 50 It would take 8.4 years (observed), but only is 3.6 years (relative).

STEP 2: Once you have STEP 1 working, modify your module so that it displays travel time for a number of different velocities. You should place your statements that calculate and print the observed and relative times inside a for loop, which ranges over a number of velocities. Your list of velocities must include 10%, 25%, 50%, 75%, 90% and 99%. For example:

Enter a distance (in light years): 4.2 VELOCITY OBSERVED RELATIVE 10% of light 42.0 years 4.2 years 25% of light 16.8 years 4.1 years 50% of light 8.4 years 3.6 years 75% of light 5.6 years 2.8 years 90% of light 4.7 years 1.8 years 99% of light 4.2 years 0.6 years

Hint: To round a number to a certain precision, use the round function with a second parameter to specify the number of digits to the right of the decimal place. For example, round(123.456, 1) evaluates to 123.5, while round(123.456, 2) evaluates to 123.46.

Hint: To arrange the output in columns, you can insert a tab character (\t) between output values. For example, print(12, "\t", 15) would print the two numbers with a tab stop in between.

## 3. Monogrammatic Terrapins

The Hello, Little Turtles chapter of the online text contains numerous examples of using a turtle to draw patterns on a turtle graphics window. This last part will involve using a turtle to draw your initials using commands such as left, right, forward, up, down and goto.

STEP 1: You are to write a Python module, named lastnameInitials.py (where lastname is your last name). When executed, your module should draw your initials starting at the center of the turtle graphics window. Your initials should be level (recall that the turtle starts out facing to the right). For example:

STEP 2: Once you have STEP 1 working, modify your module so that it prompts the user for a number, then displays your initials that many times at random positions in the window. You will need to place your statements for drawing the initials inside a for loop, which will repeat the specified number of times. For example, if the user entered 10 at the prompt, the following might be drawn:

Hint: To generate a random position, you will need to use the goto turtle command and the randint function that is defined in the random module. This means that you will have to add an import random statement at the top of the module, then call the randint function from that module to generate random coordinates. For example, random.randint(-300, 300) will evaluate to a random integer in the range -300 to 300.