Python Learning 1.3 - Example 1: Temperature Conversion

"Temperature conversion" problem analysis

temperature conversion

Two different systems for temperature characterization

  • Celsius:
    China and most other countries in the world use
    the freezing point of 0 degrees and the boiling point of 100 degrees at 1 standard atmosphere to describe the temperature in equal parts.

  • Fahrenheit:
    The United States, the United Kingdom and other countries use
    the freezing point of 32 degrees and the boiling point of 212 degrees at 1 standard atmosphere to divide the temperature into equal parts.

demand analysis

  • Celsius to Fahrenheit conversion
  • Convert Fahrenheit to Celsius

problem analysis

Understanding and determining the computational part of the problem

  • Understanding 1:
    Directly convert the temperature value

  • Comprehension 2:
    Understand and convert the sound or image form of temperature information released

  • Understanding 3:
    Monitor temperature information release channels, obtain and convert temperature values ​​in real time

analyse problem

Since we are still in the introductory stage, we use Understanding 1Convert temperature values ​​directly

  1. The temperature value needs to indicate the temperature system, that is, Celsius or Fahrenheit
  2. After conversion, the temperature system also needs to be given

draw boundaries

  • Input : Temperature value with Fahrenheit or Celsius notation
  • Processing : Select the appropriate temperature conversion algorithm based on the temperature flag
  • Output : Temperature value with Celsius or Fahrenheit

Input and output format design

means put it at the end of the temperature, F means Fahrenheit, C means Celsius

82F means 82 degrees Fahrenheit, 28C means 28 degrees Celsius

design algorithm

According to the definition of Fahrenheit and Celsius, the conversion formula is as follows:

C =( F - 32)/1.8 \\
F = C * 1.8 + 32

Among them, C means Celsius and F means Fahrenheit.

"Temperature conversion" example writing

Let me show you the code first. Although there may be a lot of it that you don’t understand, it doesn’t matter. The details will be explained to you one by one later.

Program source code

# TempConvert.py
TempStr = input('请输入带有符号的温度值:')
if TempStr[-1] in ['F', 'f']:
    C = (eval(TempStr[0:-1]) - 32) / 1.8
    print('转换后的温度是{:.2f}C'.format(C))
elif TempStr[-1] in ['C', 'c']:
    F = 1.8 * eval(TempStr[0:-1]) + 32
    print('转换后的温度是{:.2f}F'.format(F))
else:
    print('输入格式错误')

I hope you can run the above code yourself to deepen your impression of the code. If possible, I hope you can think about whether you can run it once to convert multiple temperatures or convert multiple temperatures at once.

"Temperature conversion" draws inferences about other cases from one instance

learn by analogy

After we finish studying a problem, we should learn to draw inferences from one example and think about this problem as an extension to other problems in the fifth grade of junior high school.

Understanding Python syntax elements

  • The temperature conversion program has a total of 10 lines of code, but contains many syntax elements
  • Clearly understand these 10 lines of code to quickly get started with the Python language
  • Reference frame structure, line-by-line analysis, word-by-word understanding

Changes in input and output

  • The design of the relationship between temperature values ​​and temperature labels can be changed
  • The logo change is placed before the temperature value: C82, F28
  • The identification character is changed to multiple characters: 82Ce, 28F

Extensions to computational problems

  • The temperature conversion problem is a representative problem of various conversion problems.
  • Currency conversion, length conversion, weight conversion, area conversion
  • Different problems, but similar code

Programming software recommendations

I forgot to recommend some programming software to you in the previous article. Because we are learning Python and are beginners, generally speaking, we use interactive programming; you can use IDLE, Jupyter
notebook or VScode. , but it is more recommended to use Anaconda. Anaconda’s data analysis effect is particularly good.

If you want to do some engineering projects in the future, it is recommended to use Pycharm;
I use both VScode and Pycharm, because using VScode on small scripts can save a lot of time, and Pycharm's unique functions are also very good when adjusting the backend.

You can explore the download method by yourself. The official download URL and some small suggestions are given below.

https://code.visualstudio.com/ VScode download address
https://www.anaconda.com/ Anaconde download address
https://www.jetbrains.com/pycharm/ Pycharm download address

VScode

Because I use VScode, here are some small plug-ins recommended.

Insert image description here
The place to find the extension is right here.

1. Chinese (Simplified) Language Pack for Visual Studio Code

Insert image description here
This plug-in is suitable for the Chinese (Simplified) language pack of VS Code and provides a localized interface for VS Code so that even English novices can understand the interface.

2、Code Runner

Insert image description here
This plug-in is also a must-install plug-in. Its function is to run code snippets or code files in multiple languages.

3、Python

Insert image description here
This is definitely indispensable to learning Python, but let’s take a look at the official explanation.

Python extension for Visual Studio Code Visual
Studio Code extension with rich support for the Python language (for all actively supported language versions: >=3.7), including IntelliSense (Pylance), linting, debugging, code navigation, code formatting, refactoring , Variable Explorer and other features, Test Explorer and more!

4、Tabnine AI Autocomplete

Insert image description here
This is a plug-in to assist in coding, using AI to write code faster.

If you feel that the original interface is not good-looking enough, you can search for Theme in the extension and change the theme.

Jupyter notebook

Because many novices are not used to file-based writing, and do not like to use IDLE, then you can use notebook, which can not only write code, but also take notes, killing two birds with one stone.

Download notebook

After you download the Python interpreter, hold down win+r, open cmd, and use the pip command to download the notebook.
The specific operations are as follows:

1、win+r

Insert image description here

2、Open cmd

Insert image description here

3. Enter the pip command and press Enter

pip install Jupyter notebook

Insert image description here

4. The following line appears, indicating success.

Insert image description here

Modify the working directory of the notebook

By default, the working directory of the notebook is in C:\user\xxx (user name), and we usually do not put files in this place, so we usually modify the working directory of the notebook; and modify the working directory of the
notebook There are two ways, one is temporary change and the other is permanent change.

Temporary modification

1. Enter the desired working directory

Insert image description here

2. Open notebook

Insert image description here
In this way, you can temporarily modify the open position of the notebook, but this is only a temporary modification.

Permanent modification

1. Enter the command in cmd to create the default writing configuration of the notebook.

jupyter notebook --generate-config

Insert image description here

2. Open the created file with a text editor

You only need to follow the path to find the default configuration file, and then use VScode or Pycharm to open it.
The demonstration here uses Pycharm

Insert image description here

3. Ctrl+f to search

Find the file fornotebook_dirThe place

Don't worry, it's just one place, you won't find the wrong one

Insert image description here

4. Change directory address

Enter your desired working directory in the following single quotes, and then replace the#Remove it, otherwise it will not take effect.

Insert image description here

summary

The above is the entire content of this blog post, and I hope you can learn a little bit; the
blogger is not as knowledgeable as you think, and I am just a student. I hope you will find this blog post useful. usefulness.
Anaconda is not introduced at the end of this article not because I think it is not easy to use, but because I have no need for data analysis yet. However, I still strongly recommend novices to use Anaconda because its environment is really helpful for novices. Very very friendly.
Thank you all again for seeing this, thank you!

Guess you like

Origin blog.csdn.net/m0_68192925/article/details/125764582