Skip to content

Instantly share code, notes, and snippets.

@ejmurray
Created February 25, 2015 13:38
Show Gist options
  • Save ejmurray/9512b5d69310c1dd2d69 to your computer and use it in GitHub Desktop.
Save ejmurray/9512b5d69310c1dd2d69 to your computer and use it in GitHub Desktop.
Updated temp conversion
#!/usr/bin/python
# encoding: utf-8
"""
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 = raw_input('Enter selection: ')
while which != 'F' and which != 'C':
which = raw_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