Question

Using Python (Spyder), Write a function convert_temperature that takes two arguments: a float parameter temp, the...

Using Python (Spyder),

Write a function convert_temperature that takes two arguments: a float parameter temp, the temperature to convert; and a str parameter conversion that determines which conversion to perform. Assume that conversion can take two possible values, 'celsius_to_fahrenheit' and 'fahrenheit_to_celsius', and make 'celsius_to_fahrenheit' the default value. Have your function print out a sentence explaining the conversion done, such as “0 °C converts to 32 °F”, and have the function return the converted value.

Your submission should include the function definition of followed by printing the result of three function calls: one that uses the default conversion type, and two that explicitly specify the conversion type, one for each possible conversion (Celsius to Fahrenheit and vice versa). You may (but are not required to) query the user for the input temperatures

Homework Answers

Answer #1
PYTHON CODE:

def convert_temperature(temp, conversion = "celsius_to_fahrenheit"):
  if(conversion == "celsius_to_fahrenheit"):
    tempF = (temp * 9/5) + 32
    print(str(temp) + " °C converts to " + str(tempF) + " °F")
    return tempF
  else:
    tempC = (temp - 32) * 5/9
    print(str(temp) + " °F converts to " + str(tempC) + " °C")
    return tempC

# Testing above method
temp = 0
# Test 1: default conversion
convert_temperature(temp)
# Test 1: Celsius to Fahrenheit
convert_temperature(temp, "celsius_to_fahrenheit")
# Test 1: Fahrenheit to Celsius
convert_temperature(temp, "fahrenheit_to_celsius")

OUTPUT:

Know the answer?
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
Similar Questions
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
1. Design a function called sum that will take as arguments two floating-point numbers and will...
1. Design a function called sum that will take as arguments two floating-point numbers and will return the sum of those two numbers. 2. Design a function called get_longer that will take as arguments two phrases (strings) and will return the longer of the two strings. 3. Design a function called get_hypotenuse that will take as arguments the two lengths of the short sides (a and b in the formula below) of a right-angle triangle and returns the length of...
WRITE IN PYTHON Using any large text file or any literature English book in .txt format....
WRITE IN PYTHON Using any large text file or any literature English book in .txt format. The program will read a .txt file and process the information Write a module called, “book_digest”. The module must have the following functions: ● digest_book(file_path: str) -> None ○ This function collects and stores the required information into global dictionaries, lists, and variables. The file (book) is read and parsed only one time then closed ○ This function should raise a FileNotFoundError exception if...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT