CSC 221: Introduction to Programming
Fall 2023

HW 2: Python Modules

This assignment will involve writing three small Python modules. These modules are similar to the examples from class in that they prompt the user for input values, perform calculations on those values, and display the results in a message. 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.

When you have completed and thoroughly tested all three modules, you should compress the three module files into a single ZIP file and submit that ZIP file via BlueLine.

1. Wheels Go Round and Round

When you were a child, you may have sung the song "The wheels on the bus go round and round." This rather annoying song consisted of verses all of the same general form. For example,

    The wheels on the bus go round and round,
    round and round, round and round.
    The wheels on the bus go round and round,
    all through the town.
    
    The wipers on the bus go swish swish swish,
    swish swish swish, swish swish swish.
    The wipers on the bus go swish swish swish,
    all through the town.

You are to write a Python module, named lastnameBus.py (where lastname is your last name). When executed, your module should prompt the user for the bus part and its sound. It should display the corresponding verse, broken onto four lines as shown above. For example:

Enter the bus part: wheels What sound does it make? round and round The wheels on the bus go round and round, round and round, round and round. The wheels on the bus go round and round, all through the town.

Hint: When you display multiple values in a print statement, the values are separated by a space. If you want to print two string values without an intervening space, you will need to concatenate them. For example, print("a", "b") will print a b, while print("a"+"b") will print ab.

Hint: To produce a blank line between the input and output (as shown), simply call print without any inputs: print().

2. Play Ball

In the past few 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: 560 Enter the number of runs allowed: 489 The expected winning percentage is 0.5673748600107469 Over a full season, that projects to 92-70

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: If you want to print a number and a string without spaces in between, you must convert the number to a string then concatenate. For example, print(str(12)+"!") will print 12!.

3. 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:

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 7.3 years (relative).

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.