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 ,000.00')
#print('Connected: True')
#print('Increase of .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')
,它正在清除您的打印输出而不是持续打印它。
所以我正在尝试创建一个比特币价格跟踪器,它会告诉你价格是上涨还是下跌。例如,当您启动它时,它会显示价格和增加 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 ,000.00')
#print('Connected: True')
#print('Increase of .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')
,它正在清除您的打印输出而不是持续打印它。