Florida Southern Sunset
CSC 2280: Intro Programming
(Fall 2024)

Syllabus

LMS

Teachers

Assignments
Assignments


Other Pages

Project 1:
Variable Effort


Assigned: Fri Aug 30 2024
Due: 11:59:00 PM on Sun Sep 01 2024
Team Size: 1
Language: Python
Out of: 30 points


In this project, we'll practice more using variables, especially parameters.

Part 0, 2 points: Let's encapsulate the solution for exercise 2.2 into a function. Create a function, print_sphere_volume_radius_5(), which prints the volume of a sphere with radius 5 like this:

>>> print_sphere_volume_radius_5()
A sphere with radius 5 has volume 523.5987755982989 .
Your code should not simply be one line that prints this string; it should perform the complete calculation. To make sure you're doing that right, I recommend starting like the following:
def print_sphere_volume_radius_5():
    '''Prints a string describing the volume of a sphere.'''
    pi = 3.1415926535897931
    radius = 5
    volume = #use radius and pi in the volume formula here
    print('A sphere with radius ' + str(radius)) #finish this
(Please use the value of pi I included above. In the future, we will use the math package's pi as a better approximation, but for now use this one.) There will be lots of questions where you have to output a sentence. For these:
  • Make sure you have all the words in the correct places as in the example.
  • Don't forget the period at the end of the sentence.
  • Make sure your capitalization is correct.
You might even be able to improve your output using string concatenation to remove the space before the final period. It's not necessary to do this, but if you do, that's excellent! Tester.

Part 1, 2 points: Create a new function, print_sphere_volume_radius_8(), which prints the volume of a sphere with radius 8 in the same way as the last function. To write this, you should be able to copy-paste the last function and just change one character! Once you think you have it working, add the test like we did before. Then cross-reference the result of the tests with people around you. (Don't show them the code in your script! Just show the output in the terminal and see if you both get the same thing.) Tester.

Part 2, 4 points:

Lets take the prior function and improve it by making it more useful. Let's add a parameter. Write the function print_sphere_volume, which will take the radius of the sphere as a parameter. To add that parameter, add it between the parentheses in the header:
def print_sphere_volume(radius):
Inside the body of your function, you'll use that parameter in your calculations. You can assume that it has a value, because in order to run the function, the code calling it will have to supply a value there. For example:
>>> print_sphere_volume(5)
A sphere with radius 5 has volume 523.5987755982989 .
>>> print_sphere_volume(8)
A sphere with radius 8 has volume 2144.660584850632 .
If you're having trouble getting this to work, a common problem for new programmers is redefining the parameter(s) at the beginning of the function. Make sure you're not doing something like this:
def print_sphere_volume(radius):
     radius = 5 #Destroys the parameter 
     pi = 3.1415926535897931
    ...
In that first line of the body, whatever value the had been given to the radius parameter when it was called is now overwritten to 5. This function will now give the wrong answers:
>>> print_sphere_volume(5)
A sphere with radius 5 has volume 523.5987755982989 .
>>> print_sphere_volume(8)
A sphere with radius 5 has volume 523.5987755982989 .
Tester.

Part 3, 2 points: We're going to switch back to functions without parameters for a bit. Let's create a function to do part 2 of exercise 2.2. Create a function, print_total_wholesale_cost(). After the docstring (yes, you have to come up with that yourself this time) start with the following two lines:

    cover_price = 24.95
    num_copies = 60
After calculating the total cost, your function should print output like this:
The total wholesale cost of 60 books is $ 945.4499999999999 .
$24.95 is extremely cheap for a college textbook. Let's make this a bit more realistic. Change the cover price to $94.00 and the number of books ordered to 35 (cost-conscious students will share). Without changing anything else, your output should now look something like:
The total wholesale cost of 35 books is $ ****** .
(It's okay if your answer doesn't have the correct amount of digits. In fact, I'm expecting that it won't.) Write your test and check the output with other students. Tester.

Part 4, 2 points: Let's create a function to print out the sounds animals make: horses, ducks, cows, and goats. Add a new function, print_animal_sounds() and start by creating eight variables:

    horse_sound = "neigh"
    duck_sound = "quack"
    cow_sound = "moo"
    goat_sound = "bleat"
    num_horses = 2
    num_ducks = 2
    num_cows = 2
    num_goats = 2
Your function should print out all the noises assuming all the animals speak at the same time. To mitigate the beastly chaos a bit, we'll have the horses go first, then ducks, then cows, then goats. If we leave it so there are two of each animal, then the function should print:
neighneighquackquackmoomoobleatbleat
(Notice that there are no spaces between the animal noises.) Hint: try executing these lines in interactive mode:
>>> two_noises = 'moo' + 'quack'
>>> print(two_noises)
After you get that working, change your code so there are three horses, four ducks, two cows, and only one goat. You should only need to change the four numeric variables and not anything else in the code. Tester.

Part 5, 0 points: Expert Practice: let's improve part 3 of exercise 2.2. Create a function, print_breakfast_time(). Dealing with time can be a bit tricky, so I recommend starting with these variables:

    start_hours = 6
    start_minutes = 52
    easy_mile_minutes = 8
    easy_mile_seconds = 15
    tempo_mile_minutes = 7
    tempo_mile_seconds = 12
    num_easy_miles = 2
    num_tempo_miles = 3
After doing the calculations, your function should print output like this:
After running 5 miles, I returned home for breakfast at *:**:**.
You do not need to make additional changes to this one (though you hopefully see where you could make those changes). Hints: the expression a % b means only the remainder when a is divided by b and a // b means only the whole part (drops the fractional part) when a is divided by b.

Part 6, 2 points: Let's go through the process of changing a no-parameter function to one with parameters again. I like indenting with four spaces (when I'm coding). Write a new function, indent_mouse() in the following way:

def indent_mouse():
    string = 'mouse'
    print('    ' + string)
What happens if you invoke this function in your testing function? Tester.

Part 7, 2 points: Let's improve that function to use a parameter, which we will still indent by four spaces. Copy and paste the code from the previous part to duplicate the function, then rename it to indent_string. In order to have it take an argument, we need to add a parameter in the parentheses. Do that by changing the header to this:

def indent_string(string):
This function will now be called by separate code that might look like this:
>>> indent_string("Djinni Bartimaeus")
    Djinni Bartimaeus
During the execution of the function, the parameter string will be assigned the value in the parentheses (the argument) in the function call. You can use this parameter just like any other variable. It's common to want to assign that parameter a value, sort of like how our code does right now:
def indent_string(string):
    string = 'mouse'
    print('    ' + string)
Here, however, we've immediately overwritten the value of the parameter, making it useless. No matter what argument is used, this function will always print out the same thing. (Try these out!)
>>> indent_string('bananaface')
    mouse
>>> indent_string('vedalken wizard')
    mouse
That first line in the body is the culprit. It nullifies the power of the parameter, always setting it to some fixed value. Remove it! Now test your function on those same inputs. What happens when you give it different arguments? (Don't forget to add a docstring!) Tester.

Part 8, 2 points: Let's write a function to say a nice greeting to someone: greet

>>> greet('Bananaman')
Hi, Bananaman!
>>> greet('Count Duckula')
Hi, Count Duckula!
I recommend naming your parameter name, like this:
def greet(name):
    '''Prints out a greeting.'''
    ...
Tester.

Part 9, 2 points: Let's practice using multiple parameters!

def greet_whole_name(first_name, last_name):
    '''Prints a greeting to someone with two names.'''
    ...
It should work like this:
>>> greet_whole_name('Count',  'Duckula')
Hi, Count Duckula!
>>> greet_whole_name('Danger', 'Mouse')
Hi, Danger Mouse!
Tester.

Part 10, 2 points: This is a long one! Write super_brag_aboutwhich works like this:

>>> super_brag_about('Ash', 'Ketchum')
Wow, Ash Ketchum is definitely the coolest person I know.  They are just so
awesome!  Ash can jump through hoops of fire and once bested a Siberian Hors
etiger in a game of Chessboxing!
Although my example here is broken into multiple lines, you should just print everything on one line. (And Horsetiger is one word.) Tester.

Part 11, 2 points: Exercise 3.1. This one's a bit tricky. The total length of the string that you're printing should be exactly 70. (I recommend using a parameter name that's better than s, such as string). Here are some examples:

>>> right_justify('mountain')
                                                              mountain
>>> right_justify('amazing banana monkeys')
                                                amazing banana monkeys
>>> right_justify('smurf')
                                                                 smurf
Tester.

Part 12, 2 points: Let's write a function that takes two arguments, indent_strings. To take two arguments, the header needs to include two parameters, like this:

def indent_strings(string_a, string_b):
This function should indent four spaces, then print both strings, like this:
>>> indent_strings("Barb", "Kellner")
    BarbKellner
There are two different ways to write this, but I would like you to call indent_string, giving it a single argument based on your two parameters. (One function calling another is called "function composition".) Tester.

Part 13, 2 points: Let's write a function that right-justifies two lines! It should work like this:

def right_justify_two("Gus", "Chiggins"):
                                                                   Gus
                                                              Chiggins
Again, I want you to use function composition and call right_justify in your code. (You'll need to call it twice.) Tester.

Part 14, 2 points: Write a function, print_square_area(side) that prints out a sentence about the area of the square:

>>> print_square_area(1)
A square with sides of length 1 has area 1.
>>> print_square_area(3)
A square with sides of length 3 has area 9.
Tester.

Submitting your Project:

Make sure all your code is in a file labelled with your user name (everything before the @ in your school email address) followed by _projects.py all in snake_case. (For example, my file name would be: kburke_projects.py.) It's very important to name your file correctly in order for me to grade it. Make sure your code runs, then upload it to the project on Canvas. (Don't submit code that doesn't run; you won't earn any points!) Your code should include solutions to all non-zero-point problems from Project 0 onwards. If there is already a file up on Canvas, delete that before uploading the new version or make sure your new file replaces that. (Sometimes Canvas adds a number after the file name. Don't worry about that, because it's something (freaking annoying) you don't have control of. I have a script that automatically deletes that.)