This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

preface

✨ Python starter ✨ 1️ environment configuration ❤️ Hello World ✨ Python starter ✨ 1️ environment configuration ❤️ Hello World

This article I am going to write a temperature conversion of the small program:Between degrees Celsius and Fahrenheit.

Refueling 💪🏻ヾ(◍°∇ port) Blue blue

Nonsense not say, I began to directly

The text start

1 Create a project file

The first step ☝🏻, of course, is to open PyCharm and quickly create a new project, then create a Python file 😌 :

2 Clear your logic

No matter what program, my habit: logic must be clear first!!

Temperature conversion applet, to put it simply, suppose I input 100 degrees Celsius, then the program executes the program, what I expect is to return me the corresponding number of degrees Fahrenheit; And vice versa.

The logic is pretty clear:

  • Step 1: Enter a temperature number, degrees Celsius or Fahrenheit
  • Step 2: Determine whether the input temperature is Celsius or Fahrenheit
  • Step 3: If it is Celsius, you need to use the formula to convert it to Fahrenheit, and print out the calculation results
  • Step 4: If it is Fahrenheit, you need to use the formula to convert it to Celsius, and print the calculation results
  • Step 5: If you enter nothing, return an error message

That’s the simple program logic, but then you need to deal with the conversion formula and some small details, such as floating point arithmetic for decimals.

  • At 😄, the formula is simple. ° F to ° C = (ο F-32) / 1.8. On the contrary, ° C to ° F = 1.8 * ° C + 32.

  • So let’s say the Fahrenheit variable is called F, and the Celsius variable is called C.

  • Because decimals are involved, the float function is used; At this point, the logic is probably clear.

3 Programming

That next is according to the above logic to write procedures, step by step is 😎!

1. Enter a temperature, Celsius or Fahrenheit

  • As demonstrated above, a program can obtain user input values through the input function.
  • Define the val variable name to receive the value from the input function, where I can choose to enter degrees Fahrenheit or Celsius, such as 32C /32C or 32F /32F.

2. Determine whether the input temperature is Celsius or Fahrenheit

  • If… Elif is a branch judgment statement used to determine the input temperature option;
  • Val [-1] takes the last digit of the variable, so I’m going to use c/ c or F /F as the last digit.

3. If it is Celsius, it needs to be converted to Fahrenheit by the formula and printed out

  • The third line of the above 👆🏻 program actually translates the temperature conversion formula into Python and uses the assignment statement to assign the calculation result to the variable F.

  • Since the result may appear as a decimal, use a float floating-point function;

  • Val [0:-1] is a Python specific slicing operation, also known as the slicing operation, where the subscript 0 represents the first element from the left and -1 represents the last element from the bottom.

  • Finally, print the calculated results; %.2f is to reserve the floating point number float by two decimal places, with 2 indicating the number of digits to be reserved.

4. If it is Fahrenheit, it needs to be converted to Celsius by the formula and printed

  • The logic here is the same as explained in step 3, no more explanation 😏~

5. If nothing is entered, an error is returned

  • This is the simplest, I love to write, input error return prompt! 😕 So easy ~

4 Running Tests

Now that the program is finished, we must run a test wave to see if anything goes wrong:Hit Run, enter 32 degrees Celsius and return 89.60 degrees Fahrenheit. Is that right? Baidu?Looks like nothing’s wrong. That’s it! Today write this 😋, I want to continue to learn ~

The complete procedure is as follows:

val = input("Please enter a temperature value with a temperature symbol (e.g. 32C) :")
if val[-1] in ['C'.'c']:
    f = 1.8 * float(val[0: -1]) + 32
    print("Temperature after conversion: %.2ff" % f)
elif val[-1] in ['F'.'f']:
    c = (float(val[0: -1]) - 32) / 1.8
    print("Temperature after conversion: %.2fc" % c)
else:
    print("Input error")
Copy the code

Write in the last

Praiseworthy is persistence! More practice will pay off sooner or later ~❤️


This is the end of sharing ~

If you think the article is helpful to you, please like it, favorites it, pay attention to it, comment on it, and support it four times with one button. Your support is the biggest motivation for my creation.