Skip to content

Instantly share code, notes, and snippets.

@IanMcT
Created November 7, 2016 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanMcT/3b88d2a70149bf0890c5177a0f907b9f to your computer and use it in GitHub Desktop.
Save IanMcT/3b88d2a70149bf0890c5177a0f907b9f to your computer and use it in GitHub Desktop.
Demonstrates how to use methods in a program.
#I McTavish
#Nov 2, 2016
#Demonstrate methods with temp convertor
def convert_c_to_f(c):
"""Method to convert Celsius to Fahrenheit
Pre: Celsius temp
Post: Fahrenheit temp"""
f = 9/5*c+32
return format(f,".2f")
def convert_f_toc(f):
"""Method to convert Fahrenheit to Celsius
Pre: Fahrenheit emp
Post: Celsius temp"""
c = 5/9*(f-32)
return format(c,".2f")
#Main Program
"""Loop until user enters q to quit - get temp and convert"""
user_input = input("Pick a choice: \n1 - convert C to F\n2 - convert F to C\n3 - Quit\n")
while user_input != "3":
if user_input == "1":
temp_c = input("Enter a temperature in Celcius\n")
if str.isdecimal(temp_c):
print("In fahrenheit:",convert_c_to_f(float(temp_c)))
else:
print("Sorry, a number must be provided.")
elif user_input == "2":
temp_f = input("Enter a temperature in Fahrenheit\n")
if str.isnumeric(temp_f):
print("In Celsius:",convert_f_toc(float(temp_f)))
else:
print("Sorry, a number must be provided.")
else:
print("Not a valid choice.")
user_input = input("Pick a choice: \n1 - convert C to F\n2 - convert F to C\n3 - Quit\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment