Created
February 24, 2015 13:56
-
-
Save ejmurray/005f46c8e9e94ef0197c to your computer and use it in GitHub Desktop.
temperature conversion program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# encoding: utf-8 | |
# TODO fix the getconverto() function | |
""" | |
Temperature conversion program that converts C->F or F->C | |
C:\Users\ernest\Downloads\Introduction to Computer Science Using Python (1).pdf | |
page 174(204 in the pdf) | |
""" | |
__author__ = 'Ernest' | |
def displaywelcome(): | |
""" | |
Welcome message | |
:return: none | |
""" | |
print('This program will convert a range of temperatures') | |
print('Enter (F) to convert Fahrenheit to Celsius') | |
print('Enter (C) to convert Celsius to Fahrenheit\n') | |
def getconverto(): | |
""" | |
Figures out which units you want to convert to. | |
:return: which units you want to output | |
""" | |
which = input('Enter selection: ') | |
while which != 'F' and which != 'C': | |
which = input('Enter selection: ') | |
return which | |
def displayfarentocelsius(start, end): | |
""" | |
:param start: start of the temperature range | |
:param end: end of the temperature range | |
:return: the temperature range | |
""" | |
print('\n Degrees', ' Degrees') | |
print('Fahrenheit', 'Celsius') | |
for temp in range(start, end + 1): | |
converted_temp = (temp - 32) * 5/9 | |
print(' ', format(temp, '4.1f'), ' ', format(converted_temp, '4.1f')) | |
def displaycelsiustofahrenheit(start, end): | |
""" | |
:param start: start of tem range | |
:param end: end of the temp range | |
:return: returns the list of temperatures | |
""" | |
print('\n Degrees', ' Degrees') | |
print('Celsius', 'Fahrenheit') | |
for temp in range(start, end + 1): | |
converted_temp = (5/9 * temp) + 32 | |
print(' ', format(temp, '4.1f'), ' ', format(converted_temp, '4.1f')) | |
# main program | |
# display the welcome message | |
displaywelcome() | |
# get the conversion from the user | |
which = getconverto() | |
# get range of temperatures to convert | |
temp_start = int(input('Enter staring temperature to convert: ')) | |
temp_end = int(input('Enter the ending temperature to convert: ')) | |
# display the range of converted temperatures. | |
if which == 'F': | |
displayfarentocelsius(temp_start, temp_end) | |
else: | |
displaycelsiustofahrenheit(temp_start, temp_end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment