Question

Please help! CS Problem Set 5 #Write a function called sphere_data. volume_and_area will #take in a...

Please help! CS Problem Set 5

#Write a function called sphere_data. volume_and_area will
#take in a dictionary. This dictionary is guaranteed to
#have exactly one key: "radius", whose value is an integer
#representing the radius of a sphere.
#
#Modify this dictionary to add two keys: "volume" and "area".
#The values associated with these keys should be the volume
#and surface area of the sphere.
#
#The formula for volume is:
# (4/3) * pi * radius ^ 3
#
#The formula for surface area is:
# 4 * pi * radius ^ 2
#
#Both volume and surface area should be rounded to two
#decimal places. You can do this with round(val, 2).
#
#The line below will let you use pi as a variable in your
#code, with a value of pi to the 15th decimal place.
from math import pi

#Add your code here!


#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print 4.19 and 12.57, each on a different line.
sphere = {"radius": 1}
sphere = {"radius": 1}
sphere = sphere_data(sphere)
print(sphere["volume"])
print(sphere["area"])

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below::

#Write a function called sphere_data. volume_and_area will
#take in a dictionary. This dictionary is guaranteed to
#have exactly one key: "radius", whose value is an integer
#representing the radius of a sphere.
#
#Modify this dictionary to add two keys: "volume" and "area".
#The values associated with these keys should be the volume
#and surface area of the sphere.
#
#The formula for volume is:
# (4/3) * pi * radius ^ 3
#
#The formula for surface area is:
# 4 * pi * radius ^ 2
#
#Both volume and surface area should be rounded to two
#decimal places. You can do this with round(val, 2).
#
#The line below will let you use pi as a variable in your
#code, with a value of pi to the 15th decimal place.
from math import pi

#Add your code here!
def sphere_data(sphere):
data = {}
radius = float(sphere["radius"])
volume = 4*pi*radius*radius*radius/3
area = 4*pi*radius*radius
data["volume"] = round(volume, 2)
data["area"] = round(area, 2)
return data
  
  

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print 4.19 and 12.57, each on a different line.
sphere = {"radius": 1}
sphere = {"radius": 1}
sphere = sphere_data(sphere)
print(sphere["volume"])
print(sphere["area"])

output:

Add a comment
Know the answer?
Add Answer to:
Please help! CS Problem Set 5 #Write a function called sphere_data. volume_and_area will #take in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Nay programming languge is fine (MatLab, Octave, etc.) This is the whole document this last picture...

    Nay programming languge is fine (MatLab, Octave, etc.) This is the whole document this last picture i sent is the first page 5. Write and save a function Sphere Area that accepts Radius as input argument, and returns SurfaceArea (4 tt r) as output argument. Paste the function code below. Paste code here 6. Validate your Sphere Area function from the command line, checking the results against hand calculations. Paste the command line code and results below. 7. Design an...

  • This script will create a dictionary whose keys are all the directories listed in thePATH system...

    This script will create a dictionary whose keys are all the directories listed in thePATH system variable, and whose values are the number of files in each of these directories. The script will also print each directory entry sorted by directory name. The script will use the following five functions to get the required results get_environment_variable_value() get_dirs_from_path() get_file_count() get_file_count_for_dir_list() print_sorted_dictionary() get_environment_variable_value() The header for this function must be This function must accept a shell variable as its only parameter The...

  • Can someone help me with this coding question. I cannot get this to run properly. Write...

    Can someone help me with this coding question. I cannot get this to run properly. Write a program that defines the named constant PI, const double PI = 3.14159;, which stores the value of π. The program should use PI and the functions listed in Table 6-1 to accomplish the following: Output the value of √π . Prompt the user to input the value of a double variable r, which stores the radius of a sphere. The program then outputs...

  • Write a complete program utilizing functions. The function accepts two parameters: one representing a radius of...

    Write a complete program utilizing functions. The function accepts two parameters: one representing a radius of type float, the other parameter is an integer “select” which chooses between finding the area of a circle or the volume of sphere. If the select variable is 1, then area is calculated. If the select value is 2, then volume is calculated. If the select value is any other value, then the function returns 0. Test your function in main by calling it...

  • Create a new program in Mu and save it as ps4.5.3.py and take the code below...

    Create a new program in Mu and save it as ps4.5.3.py and take the code below and fix it as indicated in the comments: # In the Pokemon video game series, every Pokemon has six # stats: HP, Attack, Defense, Special Attack, Special Defense, # and Speed. # # Write a function called total_stats that will take as input # a list of dictionaries. Each dictionary will have seven # key-value pairs: # # - name: a Pokemon's name #...

  • Problem: Write a short C++ program that gets the side of a cube and the radius...

    Problem: Write a short C++ program that gets the side of a cube and the radius of a sphere from the keyboard and writes to a file the surfaces of these shapes.             ------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below. ------------------------------------------------------------------------------------------------------------------------ Part A (79 points) Algorithm solution (in pseudocode): To accomplish this task, your program will have to take the following steps: 1. Declare a variable named outFile that represents an output stream. 2. Declare a...

  • Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys...

    Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys that are found in both dictionaries and returns a new dictionary that contains key:value pairs where the key in the new dictionary is the key found in both dictionaries, dictionl and diction2, and the new key's value being a list of the values of dictionl.key and diction2.key values concatenated together. Assumptions 1) 2) Both dictionaries can be empty and an empty dictionary is returned...

  • Create a new program in Mu and save it as ps4.5.1.py and take the code below...

    Create a new program in Mu and save it as ps4.5.1.py and take the code below and fix it as indicated in the comments: # Write a function called phonebook that takes two lists as # input: # # - names, a list of names as strings # - numbers, a list of phone numbers as strings # # phonebook() should take these two lists and create a # dictionary that maps each name to its phone number. For #...

  • You should implement several functions that the Phone Contacts program will need Each function with take...

    You should implement several functions that the Phone Contacts program will need Each function with take in data through the parameters (i.e., variables names listed in parentheses) and make updates to data structure that holds the contact information, return, or print information for a particular contact. The parameters needed are listed in the table below and each function is described below that 1. Make an empty dictionary and call it 'contacts'. Where in your code would you implement this? Think...

  • Use Python 3, please type. Include the recommended comments at the top of your code with...

    Use Python 3, please type. Include the recommended comments at the top of your code with one test run. Calculate the area of a circle for each radius value from 100 to 500 in steps of 25 example: for i in range(1,11,3): print(i) The formula for area of a circle is pi * radius * radius use 3.142 for pi The output should look like this: radius area 100 ? 125 ? ... 500 ? Note, this program will output...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT