比特币买卖彩色显示器(cmd/win10)

2024-06-16 12:07:25 发布

您现在位置:Python中文网/ 问答频道 /正文

This is my goal ;) 我尝试编写一个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")

Tags: 代码fromimportgtjsonurlinit价格
1条回答
网友
1楼 · 发布于 2024-06-16 12:07:25

在你的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")

相关问题 更多 >