2017-07-25 133 views
-3

我需要通過間隔從網站獲取信息。我寫了兩個循環到對方。不知何故,第二個條件,而循環不起作用,並使其成爲一個無限循環。雖然價值觀似乎相同。他們甚至計算。我究竟做錯了什麼?不條件不能在python for循環中工作

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) 
+1

你怎麼能指望什麼,但一個無限循環,如果你寫的'而TRUE'永不'break'循環?你認爲那裏的停車條件是什麼? –

+0

* int(tidnew)*是無用的語句,當結果是無處分配的。 – guidot

回答

0

您的意思是?

while tid != tidnew: 
    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.") 

我不知道你的情況應該是什麼,但「真」是一個無限循環。

+0

如果程序輸出「Tid是相等的」。 ,循環將完成。如果不是這種情況是錯誤的。但是!=運算符沒有問題。 – BrutalGames

0

我會檢查哪些數據類型tidtidnew是。

我認爲tidnew = json_orderline["tid"]設置tidnew爲一個字符串,而tid是一個整數。改爲使用tidnew = int(json_orderline["tid"])。 (或者在閱讀json數據後添加tidnew = int(tidnew))。

0

當我發佈了這個內容後,我已經閱讀了第一個答案,它對我產生了影響。我試圖儘可能清楚地解釋我的問題: 此代碼的原因是從交易網站拉交易。對於答案,我收到一份包含交易信息的大量交易列表。代碼首先拉它,然後開始比較列表中的第一個「tid」條目是否相同,如果不是,則它從第一個列表條目中取出一些數據,然後移動到下一個,直到找到匹配的「tid」。 我的問題是我用wromg值覆蓋了tid條目。它應該是第一個入口,什麼是最新的交易。但是它寫了最後一個匹配的「tid」,並使這些值不相等。 魔術發生在tidfirst變量中。 新更新的代碼:

import requests 
import time 

buy = 0.0 
sell = 0.0 
tidnew = 0 
tid = 0 
tidfirst = 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] 
    tidfirst = json_orderline["tid"] 
    tidnew = json_orderline["tid"] 
    counter += 1 

    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 = tidfirst 
    print("tid is: " + str(tid)) 
    tid = int(tid) 
    counter = -1 
    time.sleep(1)