Not条件在python for循环中不起作用

2024-04-20 13:42:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要每隔一段时间从网站上获取信息。我给对方写了两个循环。不知何故不在第二个条件,而循环不工作,使它成为一个无限循环。虽然价值观似乎是一样的。他们甚至计算。我做错什么了?你知道吗

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)

Tags: jsoniscounterbuyfloatamountintprint
3条回答

你的意思是?你知道吗

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.")

我不知道while的条件应该是什么,但“while True”是一个无限循环。你知道吗

我要检查一下tidtidnew是什么数据类型。你知道吗

我认为tidnew = json_orderline["tid"]tidnew设置为字符串,而tid是整数。尝试改用tidnew = int(json_orderline["tid"])。(或者在读取json数据之后添加tidnew = int(tidnew)

当我把这个贴出来,我读了第一个答案后,它就出现了。我尽量把我的问题解释清楚: 此代码的原因是从交易网站中提取交易。作为答案,我得到了一份包含交易信息的大量交易清单。代码首先提取它,然后开始比较列表中的第一个“tid”条目是否相同,如果不相同,则从第一个列表条目中提取一些数据,然后移动到下一个条目,直到找到匹配的“tid”。 我的问题是我用wromg值重写了tid条目。它应该是第一个入口,什么是最新的贸易。但它编写了最后一个匹配的“tid”,这使得值不相等。 奇迹发生在第一个变量上。 新的更新代码:

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)

相关问题 更多 >