因此,我正在尝试创建一个比特币价格跟踪器,并使用它来告诉您价格是上涨还是下跌。例如,当您启动它时,它会显示价格和涨幅$ 0.00,然后当它增加或减少时,它会显示价格。问题在于再次测试后,它恢复为0,而不是保持增加或减少的数量。
这里我尝试了所有内容,但一切正常,但是当它更改时,只会显示一秒钟,直到测试更改为止。
###############
import requests
import time
import os
#############
#######
bct = 0.0
bctChange = 0
errorLevel = 0
################
os.system('cls')
################
#print('The current price of Bitcoin is $10,000.00')
#print('Connected: True')
#print('Increase of $2.50') #use abs() for absolute value
#print('ErrorLevel: 0')
#print('ErrorLevel is the amount of request errors\nthere have been to the bitcoin api')
########################
def place_value(number):
return ("{:,}".format(number))
#####################################################################
r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
bctPriceStart = place_value(round(r.json()['bpi']['USD']['rate_float'], 2))
###########################################################################
while True:
try:
#############
bctLast = bct
#####################################################################
r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
bct = round(r.json()['bpi']['USD']['rate_float'], 2)
####################################################
if (bctChange != bctChange):
bctChange = bctLast - bct
###########################
################
os.system('cls')
print('The current price of Bitcoin is $' + place_value(bct))
#############################################################
#################
if (bctLast > 0):
print('Increase of $' + place_value(abs(round(bctChange, 2))))
time.sleep(1)
###############
###################
elif (bctLast < 0):
print('Decrease of $' + place_value(abs(round(bctChange, 2))))
time.sleep(1)
###############
except requests.ConnectionError or requests.ConnectTimeout or requests.HTTPError or requests.NullHandler or requests.ReadTimeout or requests.RequestException or requests.RequestsDependencyWarning or requests.Timeout or requests.TooManyRedirects:
#Do error function
os.system('cls')
print('There was an error...')
答案 0 :(得分:0)
如果我了解您要执行的操作,则可以使用此脚本替换脚本:
import requests
import time
bct = 0.0
bctChange = 0
def place_value(number):
return ("{:,}".format(number))
r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
bct = round(r.json()['bpi']['USD']['rate_float'], 2)
print('The starting price of Bitcoin is $' + place_value(bct))
while True:
try:
bctLast = bct
r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
bct = round(r.json()['bpi']['USD']['rate_float'], 2)
bctChange = bctLast - bct
if bctChange > 0:
print('The current price of Bitcoin is $' + place_value(bct))
print('Increase of $' + place_value(abs(round(bctChange, 2))))
elif bctChange < 0:
print('The current price of Bitcoin is $' + place_value(bct))
print('Decrease of $' + place_value(abs(round(bctChange, 2))))
time.sleep(1)
except requests.ConnectionError or requests.ConnectTimeout or requests.HTTPError or requests.NullHandler or requests.ReadTimeout or requests.RequestException or requests.RequestsDependencyWarning or requests.Timeout or requests.TooManyRedirects:
print('There was an error...')
问题在于您的if...elif
语句基于btcLast
而不是btcChange
,并且您不需要if
周围的bctChange = bctLast - bct
语句。您还打电话给os.system('cls')
,这是在清除打印输出而不是持久打印。