In: Computer Science
Due
Sunday, November 1st at 11:59 PM
Deliverables
There is one deliverable for this assignment
Make sure the script obeys all the rules in the Script Requirements page.
Specification
Your script must print a Kelvin to Fahrenheit conversion table and between a minimum and maximum values, and a Fahrenheit to Kelvin conversion also between a minimum and maximum values.
Here is the formula for converting Kelvin to Fahrenheit
Here is the formula for converting Fahrenheit to Kelvin
The script must have 5 functions
get_int
This function must have the following header
def get_int(prompt):
The function prompts the user for a value, converts it to integer and returns the value.
kelvin_to_fahrenheit
This function must have the following header
def kelvin_to_fahrenheit(kelvin):
The function converts a Kelvin temperature to Fahrenheit and returns the Fahrenheit value.
The Fahrenheit value must be an integer.
kelvin_to_fahrenheit_table
This function must have the following header
def kelvin_to_fahrenheit_table(min, max):
The function prints a Kelvin to Fahrenheit conversion table running from min to max.
Before printing the table, the function prints the labels "Kelvin" and "Fahrenheit", followed by a line of dashes.
The Fahrenheit values should align with the "Fahrenheit" label.
See the example below.
fahrenheit_to_kelvin
This function must have the following header
def fahrenheit_to_kelvin(fahrenheit):
The function converts a Fahrenheit temperature to Kelvin and returns the Kelvin value.
The Kelvin value must be an integer.
fahrenheit_to_kelvin_table
This function must have the following header
def fahrenheit_to_kelvin_table(min, max):
The function prints a Fahrenheit to Kelvin conversion table running from min to max.
Before printing the table, the function prints the labels "Fahrenheit" and "Kelvin", followed by a line of dashes.
The Kelvin values should align with the "Kelvin" label.
See the example below.
Script for this assignment
Open an a text editor and create the file hw7.py.
You can use the editor built into IDLE or a program like Sublime.
Test Code
Your hw7.py file must contain the following test code at the bottom of the file
min_kelvin = get_int("Minimum Kelvin temperature: ") max_kelvin = get_int("Maximum Kelvin temperature: ") print() kelvin_to_fahrenheit_table(min_kelvin, max_kelvin) print() min_fahrenheit = get_int("Minimum Fahrenheit temperature: ") max_fahrenheit = get_int("Maximum Fahrenheit temperature: ") print() fahrenheit_to_kelvin_table(min_fahrenheit, max_fahrenheit)
You should see something like this
python3 hw7.py Minimum Kelvin temperature: 255 Maximum Kelvin temperature: 260 Kelvin Fahrenheit ------------------ 255 -1 256 1 257 3 258 5 259 7 260 8 Minimum Fahrenheit temperature: 10 Maximum Fahrenheit temperature: 15 Fahrenheit Kelvin ----------------------- 10 261 11 261 12 262 13 263 14 263 15 264
The entries in blue are user input.
Suggestions
In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.
Write this program in a step-by-step fashion using the technique of incremental development.
Testing on Your Machine
Minimum Kelvin temperature: 255 Maximum Kelvin temperature: 260 Kelvin Fahrenheit ------------------ 255 -1 256 1 257 3 258 5 259 7 260 8 Minimum Fahrenheit temperature: 10 Maximum Fahrenheit temperature: 15 Fahrenheit Kelvin ----------------------- 10 261 11 261 12 262 13 263 14 263 15 264The entries in blue are user input.
Unix Setup
cd it116
cd hw
mkdir hw7
ls
Copy the file to Unix
Testing the script on Unix
cd it116/hw/hw7
python3 hw7.py
Minimum Kelvin temperature: 255 Maximum Kelvin temperature: 260 Kelvin Fahrenheit ------------------ 255 -1 256 1 257 3 258 5 259 7 260 8 Minimum Fahrenheit temperature: 10 Maximum Fahrenheit temperature: 15 Fahrenheit Kelvin ----------------------- 10 261 11 261 12 262 13 263 14 263 15 264The entries in blue are user input.
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote
CODE:
#Function prompts the user for a value, converts it to integer and returns the value.
def get_int(prompt):
number = input(prompt) #prompts the user for a value
number = int(number) # converts the number to integer
return number #returns the number
#Function converts a Kelvin temperature to Fahrenheit and returns the Fahrenheit value.
def kelvin_to_fahrenheit(kelvin):
fahrenheit = (kelvin - 273.15) * 9/5 + 32 #converts the kelvin temperature to Fahrenheit
return round(fahrenheit) #return the fahrenheit temperature
#Function prints a Kelvin to Fahrenheit conversion table running from min to max.
def kelvin_to_fahrenheit_table(min, max):
print("Kelvin Fahrenheit\n------------------")
for temp in range(min, max+1): #loop through kelvin values from min to max.
print(temp, end = "\t") #print kelvin temperature
print(kelvin_to_fahrenheit(temp)) #calling kelvin_to_fahrenheit() and display the Fahrenheit temperature
#Function converts a Fahrenheit temperature to Kelvin and returns the Kelvin value.
def fahrenheit_to_kelvin(fahrenheit):
kelvin = (fahrenheit - 32) * 5/9 + 273.15 #converts the Fahrenheit temperature to kelvin
return round(kelvin) #return the kelvin temperature
#Function prints a Fahrenheit to Kelvin conversion table running from min to max.
def fahrenheit_to_kelvin_table(min, max):
print("Fahrenheit Kelvin\n-----------------------")
for temp in range(min, max+1): #loop through Fahrenheit values from min to max.
print(temp, end = "\t ") #print Fahrenheit temperature
print(fahrenheit_to_kelvin(temp)) #calling fahrenheit_to_kelvin() and display the Kelvin temperature
#program starts here
min_kelvin = get_int("Minimum Kelvin temperature: ")
max_kelvin = get_int("Maximum Kelvin temperature: ")
print()
kelvin_to_fahrenheit_table(min_kelvin, max_kelvin)
print()
min_fahrenheit = get_int("Minimum Fahrenheit temperature: ")
max_fahrenheit = get_int("Maximum Fahrenheit temperature: ")
print()
fahrenheit_to_kelvin_table(min_fahrenheit, max_fahrenheit)
OUTPUT: