Python temperature conversion instance analysis and python temperature instance analysis
This document describes how to implement temperature conversion in Python.
The Code is as follows:
# TempConvert. py val = input ("enter a temperature value (for example, 32c)") if val [-1] in ["C", "c"]: f = 1.8 * float (val [0:-1]) + 32 print ("the converted temperature is %. 2fF "% f) elif val [-1] in [" F "," f "]: c = (float (val [0:-1])-32) /1.8 print ("converted temperature: %. 2fC "% c) else: print (" incorrect input ")
Knowledge point:
1. Comment: A single line comment starts with #, and multiple lines comment starts and ends '''
2. input: <variable> = input (<prompt text>) to obtain user input and save it as a string in <variable>
3. the string length is L, the index value of the first byte is 0 or-L; the index value of the last byte is L-1 or-1
Eg. Val = "28C"
Val [-1] is the last character C;
A substring consisting of the first two characters can use Val [0: 2], which indicates the range from [0, 2;
Val [0:-1] can be used to represent strings except the last character.
4. Output: the print () function is used to output character information or output variable values in character form.
The print () function uses % to select the variable to be output.
5. Counting cycle: for I in range (<counting value>)
<Expression>
6. You can change the float () function to the eval () function: eval (str)
Eval () function: Evaluate a string expression, for example:
>>>a=1 >>>eval(“a+1”)2>>>eval(‘pow(2,2)')4
Summary
The above is all the content of this article on the Python temperature conversion instance analysis, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!