【发布时间】:2017-07-25 09:43:55
【问题描述】:
我需要按时间间隔从网站获取信息。我写了两个循环到对方。不知何故,第二个while循环中的条件不起作用并使其成为无限循环。尽管价值观似乎相同。他们甚至计算。我做错了什么?
import requests
import time
buy = 0.0
sell = 0.0
tidnew = 0
counter = -1
main_api = 'https://api.bitfinex.com/v1'
trades = '/trades/'
etc = 'ETCUSD'
getorders = main_api+trades+etc
json_orderget = requests.get(getorders).json()
json_orderline = json_orderget[0]
tid = json_orderline["tid"]
if json_orderline["type"] == 'buy':
buy = float(json_orderline["amount"])
else:
sell = float(json_orderline["amount"])
time.sleep(1)
while True:
print("first while loop")
json_orderget = requests.get(getorders).json()
json_orderline = json_orderget[0]
tidnew = json_orderline["tid"]
int(tidnew)
counter += 1
tid = int(tid)
tidnew = int(tidnew)
if tid == tidnew:
print("Tid's are equal.")
while tid != tidnew:
print("Second while loop")
json_orderline = json_orderget[counter]
price = json_orderline["price"]
tidnew = json_orderline["tid"]
if json_orderline["type"] == 'buy':
buy += float(json_orderline["amount"])
else:
sell += float(json_orderline["amount"])
print("New price is: " + str(price))
print("New tid is: " + str(tid))
print("Buy volume is: " + str(buy))
print("Sell volume is: " + str(sell))
counter += 1
tid = tidnew
print("tid is: " + str(tid))
tid = int(tid)
counter = -1
time.sleep(1)
【问题讨论】:
-
如果你写的是
while True而不是break这个循环,你怎么能期待一个无限循环呢?你认为那里的停止条件是什么? -
int(tidnew) 当结果没有赋值时是无用的语句。
标签: python while-loop