比特币卖出或买入 显示器与颜色(在cmd/win10中)



这是我的目标;) 我尝试编写一个Python脚本,该脚本打印比特币价格并将颜色设置为绿色或红色(更高的价格-->绿色/下跌价格-->红色(。

现在,它的打印全部为红色(Fore.RED(但是我该如何编写代码?

如果价格浮动更高,则 xxx 打印绿色 否则:红色

非常感谢您的帮助... :)

法典:

import requests, json
from time import sleep
from colorama import init, Fore, Back, Style
def getBitcoinPrice():
URL = 'https://www.bitstamp.net/api/ticker/'
try:
r = requests.get(URL)
priceFloat = float(json.loads(r.text)['last'])
return priceFloat
except requests.ConnectionError:
print ("Error querying Bitstamp API")
while True:
init(convert=True)
print (Fore.RED + "Bitstamp last price: $" + str(getBitcoinPrice()) + "/BTC")

在你的while循环中,我认为你想要的是:

price = getBitcoinPrice()
if price > 8000: # Or whatever price
print (Fore.RED + "Bitstamp last price: $" + str(price) + "/BTC")
else:
print (Fore.GREEN + "Bitstamp last price: $" + str(price) + "/BTC")

最新更新