Python implements exchange rate conversion operations

1 , Analysis and calculation part;

2 , Divide the problem into three parts: input, processing and output;

3 , Design algorithm for calculation.

Input: RMB amount

Processing: exchange rate calculation: input/exchange rate

Output.

Open PyCharm-File-new Project-select directory or create new, the default interpreter is Anaconda-create-whether to create in the current window or in a new window, select new window

After creation, open the directory and find that there is only one .idea file representing some configuration

Create a new file, and fill in the name: currency_converter on the top of the directory: new-Python file-the default is a .py file-OK

**Input hint: **

input('Please enter RMB amount:')

**Receive input value: **

Start a variable rmb_value_rec at the beginning and assign a value

rmb_value_rec = input('Please enter RMB amount:')
print('rmb_value_rec:', rmb_value_rec) output to see if the reception is correct (the comma entered is the English input method)

**What to do if there is text in the acceptance? **

Python has a function eval, which can only take out the part containing numbers

Define the variable to represent the input number: rmb_vlaue

rmb_value = eval(rmb_value_rec )

Exchange rate conversion:

The current exchange rate is 6.77,

Define a variable to represent the current exchange rate

us_vs_rmb = 6.77

Define a variable to represent the dollar value of the exchange rate conversion: us_value

Calculation

us_value = rmb_value / us_vs_rmb

**Result output: **

print('The dollar amount is:',us_value,'$')

run:

Right click, run

Supplementary knowledge: python uses api to get currency exchange rate

First, go to the fixer.io website, which is the website that provides currency exchange rate interfaces. The URL is: https://fixer.io/, which looks like this:

The documentation contains specific instructions for the use of the interface. In order to use this interface, you must first apply for an api access key. Click GET FREE API KEY in the upper right corner of the screen:

API keys with different prices have different functions. Of course, the more expensive the function is, the better. Here we apply for a free API KEY. The number of visits it provides is 1,000 times a month, and hourly update data can be obtained. The supported queries are limited. , For example, it can only use Euro as the base, and it can also view historical data. Click GET FREE API KEY, you need to fill in personal information:

Examples are as follows:

One of the pitfalls is that this webpage must have a vpn to be verified by man-machine. If there is no vpn, you cannot register the access key. If you use it for learning purposes, you can use Psiphon. If you only use it for personal learning, you can use my registration access key: dd44405658a1e71fd1d896a14a76f32d

You can refer to the documentation for how to use it. For example, if I want to see the current exchange rate between Euro and RMB, then I can set base=EUR, symbols=CYN, and type in the browser search bar

http://data.fixer.io/api/latest?access_key=dd44405658a1e71fd1d896a14a76f32d&base=EUR&symbols=CNY

After access_key is your registered access key, the result is as follows:

Python uses the api interface to obtain the exchange rate code as follows:

import requests
 
def main():
 res = requests.get("http://data.fixer.io/api/latest?access_key=dd44405658a1e71fd1d896a14a76f32d&base=EUR&symbols=CNY")if res.status_code !=200:
 raise Exception("ERROR: API rquest unsuccessful.")
 data = res.json()print(data)if __name__ =="__main__":main()

result:

The data obtained is in json format and can be processed later, because the base of the free key can only be euros, so you can operate it several times and then convert the exchange rate of the currency you want. Examples are as follows:

import requests
 
def main():
 base =input("First Currency: ")
 other =input("Second Currency: ")
 access_key ="dd44405658a1e71fd1d896a14a76f32d"
 res_b = requests.get("http://data.fixer.io/api/latest",
 params={"access_key": access_key,"base":"EUR","symbols": base})
 res_o = requests.get("http://data.fixer.io/api/latest",
 params={"access_key": access_key,"base":"EUR","symbols": other})if res_b.status_code !=200 or res_o.status_code !=200:
 raise Exception("ERROR: API request unsuccessful.")
 data_b = res_b.json()
 data_o = res_o.json()
 rate_b = data_b["rates"][base]
 rate_o = data_o["rates"][other]
 rate =round((rate_o / rate_b),2)print(f"1 {base} is equal to {rate} {other}")if __name__ =="__main__":main()

The above Python implementation of exchange rate conversion operations is all the content shared by the editor, I hope to give you a reference.

Recommended Posts

Python implements exchange rate conversion operations
Python implements Super Mario
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
Python implements image stitching
Python implements minesweeper game
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python implements simple tank battle
Python implements udp chat window
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python implements parking management system
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python implements image stitching function
Python implements the brick-and-mortar game