![]() | |
Teachers Assignments Assignments Other Pages | Project 1: Assigned: Fri Aug 30 2024 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()
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: (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:
Part 1, 2 points: Create a new function, 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: 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: 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, 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, 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, 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, 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 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: 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('Bananaman')
Hi, Bananaman!
>>> greet('Count Duckula')
Hi, Count Duckula! I recommend naming your parameter name , like this: Tester.Part 9, 2 points: Let's practice using multiple parameters! 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_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 >>> 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, 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: 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(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 |