当前位置:网站首页>2.6 formula calculation
2.6 formula calculation
2022-07-03 20:00:00 【leeshuqing】
1) Let the user input the content of multiplying any two integers ( Such as “2*3”) Can output “2*3=6”, For example, the operation is :

Let's finish it step by step . Complete the input first :
strs = input()
Suppose we enter “2*3”, Now we need to split the two values before and after the asterisk . This can take advantage of the string itself split Function to implement :
strs = input()
print(strs.split('*'))
The running result is :

I see ,split Method splits the current string with the characters specified in parentheses , And get the first and second strings , Namely ‘2’ and ‘3’. Two values form a list , Therefore, we can get :
strs = input()
num = strs.split('*')
print(num[0], num[1])
The running result is :

You can also use this method to obtain the two separated values :
strs = input()
a, b = strs.split('*')
print(a, b)
The running result is :

The result is no longer a list , Is the value of two variables of string type .
Please note that , This ‘2’ and ‘3’ All character types , Therefore, direct algebraic operation is not allowed :
strs = input()
a, b = strs.split('*')
print(a * b)
There is an error in the running result , The error message is that you cannot multiply non integers :

Therefore, it is necessary to convert integers and perform algebraic operations :
strs = input()
a, b = strs.split('*')
print(int(a) * int(b))
The running result is :

This result is the tail of the result we are going to output . Therefore, the previously entered content can be spliced in front of this result , Doesn't it form the final appearance ?
strs = input()
a, b = strs.split('*')
print(strs + int(a) * int(b))
There is an error in the running result , The error message means that only characters can be connected to characters :

The reason is simple ,strs For the string ,int(a) Is an integer , Of course, you can't add it directly . Further improved as :
strs = input()
a, b = strs.split('*')
print(strs + str(int(a) * int(b)))
The running result is :

Careful observation shows that , The final output is missing an equals sign , It is revised again as :
strs = input()
a, b = strs.split('*')
print(strs + '=' + str(int(a) * int(b)))
The running result is :

It can also be written as :
strs = input()
a, b = strs.split('*')
print(a + '*' + b + '=' + str(int(a) * int(b)))
The output content is the same as . This is a bit of a hassle , But the effect is the same .
In addition to these methods , We can also use another completely different method :
strs = input()
print(strs + '=' + str(eval(strs)))
If input “2*3”, The running result is :

How to understand that ?eval Function can directly replace the parameter string , And embedded in the current code . For example, if you enter “2*3”, be eval(strs) Namely 2*3, At this time, there are no quotation marks around its content , So the last line above
print(strs + '=' + str(eval(strs)))Replace with :
print(strs + '=' + str(2 * 3)) Just the right statement that can be executed directly !
2) Calculate similarity :

This is the famous cosine similarity , You can calculate the distance between two vectors . such as :

We wrote the first code :
v11 = 1
v12 = 2
v21 = 3
v22 = 4
fenzi = v11 * v21 + v12 * v22
fenmu = (v11 ** 2 + v12 ** 2) ** 0.5 * (v21 ** 2 + v22 ** 2) ** 0.5
print(fenzi / fenmu)
Output is : 0.9838699100999074
Although the writing seems very complicated , In fact, it's simple , It is to express various common algebraic operations according to the calculation formula , You can observe carefully .
For square sum square , You can also use math Module to complete :
import math
v11 = 1
v12 = 2
v21 = 3
v22 = 4
fenzi = v11 * v21 + v12 * v22
fenmu = math.sqrt(math.pow(v11, 2) + math.pow(v12, 2)) * \
math.sqrt(math.pow(v21, 2) + math.pow(v22, 2))
print(fenzi / fenmu)
The output content is the same as . among ,sqrt Is the square function ,pow Is a power function , The second parameter in parentheses represents square . Please pay attention to one detail , because fenmu The expression is very long , Used \ A newline , This symbol means that the next line is still followed by this code , Just to avoid a long line of content .
边栏推荐
- Pat grade B 1009 is ironic (20 points)
- Native table - scroll - merge function
- 2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
- Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
- Professional interpretation | how to become an SQL developer
- Use unique_ PTR forward declaration? [repetition] - forward declaration with unique_ ptr? [duplicate]
- Global and Chinese market of charity software 2022-2028: Research Report on technology, participants, trends, market size and share
- 5. MVVM model
- Exercises of function recursion
- Find a line in a file and remove it
猜你喜欢

第一章:拓广同码小数和s(d, n)

Chapitre 1: le roi de shehan a mal calculé

NFT without IPFs and completely on the chain?

PR FAQ: how to set PR vertical screen sequence?

Nerfplusplus parameter format sorting

2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程

第一章: 舍罕王失算

Make a simple text logo with DW

2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)

02 -- QT OpenGL drawing triangle
随机推荐
Pat grade B 1009 is ironic (20 points)
Make a simple text logo with DW
Realize user registration and login
About callback function and hook function
PR 2021 quick start tutorial, material import and management
Win10 share you don't have permission
Day10 -- forced login, token refresh and JWT disable
Xctf attack and defense world crypto master advanced area olddriver
AcWing 1460. Where am i?
Point cloud data denoising
Professional interpretation | how to become an SQL developer
Chapter 2: 4-digit Kaplan number, search even digit Kaplan number, search n-digit 2-segment sum square number, m-digit ingenious square number without 0, specify the number to form a 7-digit square nu
44. Concurrent programming theory
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
第一章:拓广同码小数和s(d, n)
Chapter 1: find the factorial n of n!
QT tutorial: signal and slot mechanism
[raid] [simple DP] mine excavation
第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
Today's work summary and plan: February 14, 2022