【问题标题】:Not condition does not work in python for loop非条件在python for循环中不起作用
【发布时间】: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


【解决方案1】:

你的意思是?

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”是一个无限循环。

【讨论】:

  • 如果程序输出“Tid's are equal”。 ,循环即将结束。如果不是,则条件错误。但是 != 运算符没有问题。
【解决方案2】:

我会检查 tidtidnew 是什么数据类型。

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

【讨论】:

    【解决方案3】:

    在我发布这个并且我已经阅读了它发生在我身上的第一个答案之后。我试图尽可能清楚地解释我的问题: 此代码的原因是从交易站点中提取交易。对于答案,我得到了包含交易信息的大量交易列表。代码首先拉取它,然后开始比较列表中的第一个“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)
    

    【讨论】:

      猜你喜欢
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多