繁体   English   中英

如何存储值并在python中更新时更改它?

[英]How to store a value and change it when it is updated in python?

因此,我正在尝试创建一个比特币价格跟踪器,并使用它来告诉您价格是上涨还是下跌。 例如,当您启动它时,它会显示价格和涨幅$ 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...')

如果我了解您要执行的操作,则应使用以下内容替换脚本:

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 ,并且您不需要在bctChange = bctLast - bct周围使用if语句。 您还调用了os.system('cls') ,它清除了打印输出而不是持久打印。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM