"A temperature unit that can be linearly converted to or from Kelvins. You can reduce this by defining a Unit class: class Unit(object): The biggest problem with your code is the amount of repetition. You could even define another function for that last, complicated looking, bit: def convert(temperature, from_unit, to_unit): This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you. To_unit = input("Convert to which unit? ").lower() Temperature = float(input("What temperature in that unit? ")) With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions: from_unit = input("From which unit? ").lower() Here is a start: def temperature_C_to_K(temp_C): This way you don't have to define all conversions from all units to all other units, just to and from the base unit. Second, you should follow the single responsibility principle and define functions that do one thing, instead of having one large bunch of code.įor temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). It recommends using 4 spaces as indentation, instead of 1, which makes code a lot easier to read. Print ("In gas mark, this is ", gasmark)įirst of all, Python has an official style-guide, PEP8. If you want to convert FROM a kelvin value, press k.") If you want to convert FROM a celcius value, press c. Gwhich = input("If you want to convert FROM a fahrenheit value, press f. If you want to convert FROM a gas mark value, press g.") Kwhich = input("If you want to convert FROM a fahrenheit value, press f. If you want to convert FROM a gas mark value, press g.")įahrenheit = float(input("Input the fahrenheit value: ")) If you want to convert FROM a kelvin value, press k. Print ("Error, you didn't enter any of the specified keys")Ĭwhich = input("If you want to convert FROM a fahrenheit value, press f. The Formula Used for Temperature Conversion of Fahrenheit and Celsius is: The. Gasmark = float(input("Input the gas mark value: ")) The formula used to convert temperature from Kelvin to Celcius is C K 273.15. Kelvin = float(input("Input the kelvin value: ")) Print ("In fahrenheit, this is ", fahrenheit) If you want to convert FROM a gas mark value, press g.")Ĭelcius = float(input("Input the celcius value: ")) If you want to convert FROM a kelvin value press k. Print ("Now choose the measurement you are converting FROM")įwhich = input("If you want to convert FROM a celcius value, press c. If you want to convert TO a gas mark value, press g.") If you want to convert TO a kelvin value, press k. If you want to convert TO a celcius value, press c. Which = input("If you want to convert TO a fahrenheit value, press f. Also, is there any way to stop having to use big spaces in my input quotes to get them line by line? print ("Welcome to the temperature converter, please choose which measurement you want to convert TO") Can someone tell me how I could make my code simpler so I can add more units of temperature measurement (e.g. It includes conversions for Celsius, Fahrenheit, kelvin and gas mark. Symbols, abbreviations, or full names for units of length,Īrea, mass, pressure, and other types.I am currently creating a program that can convert different units of measurement for temperature. You can find metric conversion tables for SI units, as wellĪs English units, currency, and other data. The Celsius temperature scale was designed so that the freezing point of water is 0 degrees, and the boiling point is 100 degrees at standard atmospheric pressure.Ĭonversion calculator for all types of measurement units. The degree Celsius (☌) is a unit of temperature named after the Swedish astronomer Anders Celsius (1701–1744), who first proposed a similar system in 1742. The Celsius temperature scale is now defined in terms of the kelvin, with 0 ☌ corresponding to 273.15 kelvins, approximately the melting point of water under ordinary conditions. It is defined by two facts: zero kelvins is absolute zero (when molecular motion stops), and one kelvin is the fraction 1/273.16 of the thermodynamic temperature of the triple point of water. The kelvin (symbol: K) is the SI unit of temperature, and is one of the seven SI base units. You can do the reverse unit conversion fromĭegree Celsius to k, or enter any two units below: Enter two units to convert From:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |