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.
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:
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()
.
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:
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:
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.
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.