In: Computer Science
(Python)
Write a code converting degrees in Fahrenheit to degrees Kelvin, using this conversion equation
conversion from degrees Fahrenheit to Celcius:
? = (? − 32) x 5/9
and then to degrees Kelvin:
? = ? + 273.15
with the following properties
1) the code should take into account that temperature in degrees Kelvin cannot be smaller than zero. In other words, your code should return 0 Kelvin as the smallest temperature for any input value of degrees Fahrenheit).
2) arrange calculation in a loop and print out a table showing equivalent temperatures in Fahrenheit, Celcius and Kelvin in one line (outputing only numbers) starting from 100 Fahrenheit down until temperature in Kelvin becomes 0, using step of 5 degrees Fahrenheit.
Hint: you can use tab '\t' in your print statement to line up different numbers in your code.
3) rewrite this code to compute temperature in degrees Fahrenheit given an input in degrees Kelvin. Compute temperature of the surface of the Sun (≈6600 degrees Kelvin) in Fahrenheit.
The indentation in the answering window sometimes disappear automatically due to some technical issue on the website, please refer to the screenshots for indentation.
If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.
Solution :
# part 1
# input the temperature in Fahrenheit
F = float(input("Enter the temperature in Fahrenheit: "))
# Convert to Celcius
C = (F-32)*(5/9)
# Now convert to Kelvin
K = C + 273.15
# if K is less than 0 then make K = 0
if K<0:
K=0
# display the result
print("Temperature in Kelvin:",round(K,2))
# part 2
print("")
# print Fahrenheit, Celcius and Kelvin
print("Fahrenheit\tCelcius\t\tKelvin")
# run an infinite loop until K becomes 0
F = 100
while(True):
# calculate Celcius
C = (F-32)*(5/9)
# calculate Kelvin
K = C + 273.15
# if K is negative then it must be 0
if K<=0:
K=0
# print Fahrenheit, Celcius and Kelvin and break from the
loop
print(round(F,2),end="\t\t")
print(round(C,2),end="\t\t")
print(round(K,2),end="\t\t")
print("")
break
# print Fahrenheit, Celcius and Kelvin
print(round(F,2),end="\t\t")
print(round(C,2),end="\t\t")
print(round(K,2),end="\t\t")
print("")
# decrement F by 5
F = F - 5
# part 3
# convert from Kelvin to Fahrenheit
# input the temperature in Kelvin
K = float(input("\nEnter the temperature in Kelvin: "))
# Convert to Celcius
C = K - 273.15
# Convert to Fahrenheit
F = C*(9/5) + 32
# display the result
print("Temperature in Fahrenheit:",round(F,2))