调用函数计算5和6的加减乘除
时间: 2024-06-08 09:09:14 浏览: 95
详解js加减乘除精确计算
下面是调用函数计算5和6的加减乘除的示例代码:
```python
import requests
def calculate(num1, num2, operator):
url = f'https://xxxxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/xxxxx/func/'
if operator == '+':
payload = {'num1': num1, 'num2': num2, 'operator': operator}
r = requests.get(url, params=payload)
return r.text.strip()
elif operator == '-':
payload = {'num1': num1, 'num2': num2, 'operator': operator}
r = requests.get(url, params=payload)
return r.text.strip()
elif operator == '*':
payload = {'num1': num1, 'num2': num2, 'operator': operator}
r = requests.get(url, params=payload)
return r.text.strip()
elif operator == '/':
payload = {'num1': num1, 'num2': num2, 'operator': operator}
r = requests.get(url, params=payload)
return r.text.strip()
else:
return 'Invalid operator'
print(calculate(5, 6, '+')) # 11
print(calculate(5, 6, '-')) # -1
print(calculate(5, 6, '*')) # 30
print(calculate(5, 6, '/')) # 0.8333333333333334
```
其中,`url` 是函数计算的接口地址,`payload` 是请求的参数,`requests.get()` 方法发送 GET 请求并获取响应结果。需要将 `xxxxx` 替换为自己的函数计算服务名称,`func` 替换为自己的函数名称。
阅读全文