Advertisement
Guest User

Python code

a guest
Jun 20th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. import re
  2.  
  3. def convert(operation, value):
  4.     value = float(value)
  5.     if operation == "1": #Checks what conversion the user has entered, calculates it and prints out the result.
  6.         print("\n")
  7.         print(value, "°C is equal to ", c_to_f(value), "°F\n")
  8.     elif operation == "2":
  9.         print("\n")
  10.         print(value, "°F is equal to ", f_to_c(value), "°C\n")
  11.     elif operation == "3":
  12.         print("\n")
  13.         print(value, " miles is equal to", m_to_km(value), " kilometers\n")
  14.     elif operation == "4":
  15.         print("\n")
  16.         print(value, " kilometers is equal to", km_to_m(value), " miles\n")
  17.     elif operation == "5":
  18.         print("\n")
  19.         print(value, " pounds is equal to", lb_to_kg(value), " kilograms\n")
  20.     elif operation == "6":
  21.         print("\n")
  22.         print(value, " kilograms is equal to", kg_to_lb(value), " pounds\n")
  23.     elif operation == "7":
  24.         print("\n")
  25.         print("At the time of writing, ", value, "Pounds was equal to ", gbp_to_eur(value), " Euros\n")
  26.     elif operation == "8":
  27.         print("\n")
  28.         print("At the time of writing, ", value, "Euros was equal to ", eur_to_gbp(value), " Pounds\n")
  29.     else:
  30.         print("Converter - please enter a number between 1 and 8.")
  31.     #This would be a lot more elegant if python had a switch function...
  32.        
  33. 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.
  34.     if argument.isalpha() or argument == "" or re.search(r"^[\w\d'-]+$", argument):
  35.         return False
  36.     else:
  37.         return True
  38.  
  39. def checkNumber(argument):
  40.     for i in range(1, len(argument)):
  41.         if argument[i].isalpha():
  42.             return False
  43.             print("Ran option 1")
  44.         else:
  45.             return True
  46.             print("Ran option 2")
  47.    
  48. def c_to_f(c): #Converts degrees Celsius to Farenheit
  49.     f = (c * 1.8) + 32
  50.     return f
  51.  
  52. def f_to_c(f): #Converts degrees Farenheit to Celsius
  53.     c = (f - 32) * (5/9)
  54.     return f
  55.  
  56. def m_to_km(m): #Converts miles to kilometres
  57.     km = m / 0.62137
  58.     return km
  59.  
  60. def km_to_m(km): #Converts kilometres to miles
  61.     m = km * 0.62137
  62.     return m
  63.  
  64. def lb_to_kg(lb): #Converts pounds to kilograms
  65.     kg = lb / 2.2
  66.     return kg
  67.  
  68. def kg_to_lb(kg): #Converts kilograms to pounds
  69.     lb = kg * 2.2
  70.     return lb
  71.  
  72. def gbp_to_eur(gbp): #Converts Pounds to Euros, using exchange rate as of 5th June 2015
  73.     eur = gbp * 1.36028
  74.     return eur
  75.  
  76. def eur_to_gbp(eur): #Converts Euros to Pounds, using exchange rate as of 5th June 2015
  77.     gbp = eur / 1.36028
  78.     return gbp
  79.  
  80. def main():
  81.     #Prints out the list of available conversions. The number in brackets allows the user to select a conversion.
  82.     print("[1] °C to °F \n[2] °F to °C \n[3] Miles to Kilometers \n[4] Kilometers to Miles")
  83.     print("[5] Pounds to Kilograms \n[6] Kilograms to Pounds \n[7] Pounds to Euros \n[8] Euros to Pounds")
  84.     operation = input("Enter a number to choose your conversion: ") #Allows the user to select what conversion they want.
  85.     value = input("Enter a value to convert: ") #Allows the user to enter a value to convert.
  86.     if checkValid(operation):
  87.         print("Please enter a number between 1 and 8!") #If the input is not valid, it prints out an error.
  88.     elif checkNumber(value) == False:
  89.         print("Please enter a whole number!")
  90.     else:
  91.         convert(operation, value) #If no problem is detected, it runs the conversion.
  92.     main()
  93. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement