Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- def convert(operation, value):
- value = float(value)
- if operation == "1": #Checks what conversion the user has entered, calculates it and prints out the result.
- print("\n")
- print(value, "°C is equal to ", c_to_f(value), "°F\n")
- elif operation == "2":
- print("\n")
- print(value, "°F is equal to ", f_to_c(value), "°C\n")
- elif operation == "3":
- print("\n")
- print(value, " miles is equal to", m_to_km(value), " kilometers\n")
- elif operation == "4":
- print("\n")
- print(value, " kilometers is equal to", km_to_m(value), " miles\n")
- elif operation == "5":
- print("\n")
- print(value, " pounds is equal to", lb_to_kg(value), " kilograms\n")
- elif operation == "6":
- print("\n")
- print(value, " kilograms is equal to", kg_to_lb(value), " pounds\n")
- elif operation == "7":
- print("\n")
- print("At the time of writing, ", value, "Pounds was equal to ", gbp_to_eur(value), " Euros\n")
- elif operation == "8":
- print("\n")
- print("At the time of writing, ", value, "Euros was equal to ", eur_to_gbp(value), " Pounds\n")
- else:
- print("Converter - please enter a number between 1 and 8.")
- #This would be a lot more elegant if python had a switch function...
- def checkValid(argument): #This is used by main() to make sure what the user enters is a number, not an empty string or a letter.
- if argument.isalpha() or argument == "" or re.search(r"^[\w\d'-]+$", argument):
- return False
- else:
- return True
- def checkNumber(argument):
- for i in range(1, len(argument)):
- if argument[i].isalpha():
- return False
- print("Ran option 1")
- else:
- return True
- print("Ran option 2")
- def c_to_f(c): #Converts degrees Celsius to Farenheit
- f = (c * 1.8) + 32
- return f
- def f_to_c(f): #Converts degrees Farenheit to Celsius
- c = (f - 32) * (5/9)
- return f
- def m_to_km(m): #Converts miles to kilometres
- km = m / 0.62137
- return km
- def km_to_m(km): #Converts kilometres to miles
- m = km * 0.62137
- return m
- def lb_to_kg(lb): #Converts pounds to kilograms
- kg = lb / 2.2
- return kg
- def kg_to_lb(kg): #Converts kilograms to pounds
- lb = kg * 2.2
- return lb
- def gbp_to_eur(gbp): #Converts Pounds to Euros, using exchange rate as of 5th June 2015
- eur = gbp * 1.36028
- return eur
- def eur_to_gbp(eur): #Converts Euros to Pounds, using exchange rate as of 5th June 2015
- gbp = eur / 1.36028
- return gbp
- def main():
- #Prints out the list of available conversions. The number in brackets allows the user to select a conversion.
- print("[1] °C to °F \n[2] °F to °C \n[3] Miles to Kilometers \n[4] Kilometers to Miles")
- print("[5] Pounds to Kilograms \n[6] Kilograms to Pounds \n[7] Pounds to Euros \n[8] Euros to Pounds")
- operation = input("Enter a number to choose your conversion: ") #Allows the user to select what conversion they want.
- value = input("Enter a value to convert: ") #Allows the user to enter a value to convert.
- if checkValid(operation):
- print("Please enter a number between 1 and 8!") #If the input is not valid, it prints out an error.
- elif checkNumber(value) == False:
- print("Please enter a whole number!")
- else:
- convert(operation, value) #If no problem is detected, it runs the conversion.
- main()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement