An example analysis of Python temperature Conversion

  • 2020-07-21 08:42:00
  • OfStack

This paper mainly studies the relevant examples of Python language temperature conversion, which are as follows.

The code is as follows:


#TempConvert.py 
val=input(" Please enter the temperature value with a temperature representation symbol (for example: 32c ) ") 
if val[-1] in ["C","c"]: 
  f=1.8*float(val[0:-1])+32 
  print(" The temperature after conversion is: %.2fF"%f) 
elif val[-1] in ["F","f"]: 
  c=(float(val[0:-1])-32)/1.8 
  print(" The temperature after conversion is: %.2fC"%c) 
else: 
  print(" Input is wrong ")

Knowledge:

1. Comments: Single-line comments begin with # and multi-line comments begin and end with ''

2. Input: < variable > =input( < Suggestive text > ), the user's input is saved in the form of a string < variable > In the

3. The length of the string is L and the index value of the first byte is 0 or -ES21en; The last byte has an index value of ES22en-1 or -1

28 C eg. Val = ""

Then Val[-1] is the last character C;

The substring of the first two characters can be Val[0:2], which represents the interval from [0,2);

Val[0:-1] can be used to represent a string with the exception of the last character

4. Output: The print() function is used to output character information or the value of a variable in character form

The print() function selects the variable to output through %

5. Counting loop: for i in range( < count > )

< expression >

6. Please replace float() with eval() :eval(str)

The eval() function evaluates an expression as a string, for example:


>>>a=1         

>>>eval( " a+1 " )

2

>>>eval( ' pow(2,2)')

4

conclusion

The above is the entire content of the Python temperature conversion example analysis, hoping to be helpful to you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: