Forum Studiologe
Forum Studiologe

Reputation: 23

python if statement fails

I'm trying to check a simple flag to be true or false, but my IF statement fails and it doesn't give me the correct result, Hope someone can help!

I'm using (in python3.6) this url

    https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc

for a get request and the data it should return looks like this (from URL in browser):

    {"success":true,"message":"","result":{"Bid":18362.00000000,"Ask":18399.00000000,"Last":18362.00000000}}

Here is my program:

    import sys
    import time
    import requests
    import json
    import os

    BTCtick = 'https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc'
    reqBTC = requests.get('https://bittrex.com/api/v1.1/public/getticker?        market=usdt-btc').text
    BTCdata = json.loads(reqBTC)

    testResult = BTCdata['success']
    print("=============================")
    print("success content: ", BTCdata['success'])
    if (testResult == 'True'):
        print("IF: success flag is TRUE")
    else:
        print("IF: success flag is FALSE")
    print("=============================")

    print("result content: ", BTCdata['result'])
    print("Bid: ", BTCdata['result'] ['Bid'], sep='')
    print("Ask: ", BTCdata['result'] ['Ask'], sep='')
    print("Last: ", BTCdata['result'] ['Last'], sep='')
    print("=============================")
    exit()

Output in the console looks like this:

    =============================
    success content:  True
    IF: success flag is FALSE
    =============================
    result content:  {'Bid': 18420.0, 'Ask': 18439.99999999, 'Last': 18439.99999999}
    Bid: 18420.0
    Ask: 18439.99999999
    Last: 18439.99999999
    =============================

Any idea what I'm missing here? I'm sure it is simple Please please help me, I'm somewhat new to python and kind find out what's wrong, I have been reading a lot the last two days, but could not find the issue!

Upvotes: 0

Views: 352

Answers (1)

alecxe
alecxe

Reputation: 473863

json.loads() would convert the true to the Python's boolean True (conversion table for the reference). You just need to check for the truthiness of the testResult value instead of comparing it with a string 'True':

if testResult:
    print("IF: success flag is TRUE")
else:
    print("IF: success flag is FALSE")

In other words, here is what happened when you compared testResult to a 'True' string:

In [1]: testResult = True

In [2]: testResult == 'True'
Out[2]: False

Upvotes: 2

Related Questions