【问题标题】:range, if then, statement to print output based on datarange, if then, 根据数据打印输出的语句
【发布时间】:2021-02-08 02:55:49
【问题描述】:
我想让该值根据指定范围内的值的输出返回响应。
import requests
import json
import time
url = 'https://api.gdax.com/products/BTC-USD/trades'
res = requests.get(url)
json_res = json.loads(res.text)
print('val ', json_res[0]['price'])
buy = range(32000, 36000)
sell = range(39000, 50000)
if json_res in buy:
print('Low')
elif json_res in sell:
print('Too High')
这是我正在寻找的输出示例
val 38000.00002545
Too High
【问题讨论】:
标签:
python
python-3.x
string
if-statement
range
【解决方案1】:
阅读注释行
import requests
import json
import time
url = 'https://api.gdax.com/products/BTC-USD/trades'
res = requests.get(url)
json_res = json.loads(res.text)
price = int(float(json_res[0]['price']) // 1) # Convert from str to float to int (to match the range value)
print('val ', price)
buy = range(32000, 39000) # I changed the max range to 39000 as the current BTC price is 38xxx
sell = range(39000, 50000)
if price in buy:
print('Low')
elif price in sell:
print('Too High')
优点:
- 我仍然使用你的
range 语法
- 我警告你当前的 BTC 价格
【解决方案2】:
import requests
import json
import time
url = 'https://api.gdax.com/products/BTC-USD/trades'
res = requests.get(url)
json_res = json.loads(res.text)
print('val ', json_res[0]['price'])
if 32000 < float(val) < 3600:
print('Low')
elif 39000 < float(val) < 50000:
print('Too High')
【解决方案3】:
import requests
import json
import time
url = 'https://api.gdax.com/products/BTC-USD/trades'
res = requests.get(url)
json_res = json.loads(res.text)
print('val ', json_res[0]['price'])
buy = range(32000, 36000)
sell = range(39000, 50000)
if float(json_res[0]['price']) in buy:
print('Low')
elif float(json_res[0]['price']) in sell:
print('Too High')
else:
if float(json_res[0]['price']) <32000:
print('Lower Than low')
elif float(json_res[0]['price']) >50000:
print('Higher Than high')
else:
print('In Between')