python如果语句失败

我想检查一个简单的标志是真或假,但我的IF语句失败,它不会给我正确的结果,希望有人可以帮助!python如果语句失败

我使用(在python3.6)这个网址

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

的GET请求,它应该(在浏览器URL)返回看起来像这样的数据:

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

下面是我的程序:

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()

输出在控制台看起来是这样的:

============================= 

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

=============================

任何想法我在这里失踪?我确信它很简单 请帮助我,我对python有点新,并且发现有什么不对,我在过去两天一直在阅读很多内容,但是找不到问题!

回答:

json.loads()会将true转换为Python的布尔值True(conversion table for the reference)。你只需要检查的testResult价值感实性,而不是它的字符串'True'比较:

if testResult: 

print("IF: success flag is TRUE")

else:

print("IF: success flag is FALSE")

换句话说,这里是当你比较testResult'True'字符串发生了什么:

In [1]: testResult = True 

In [2]: testResult == 'True'

Out[2]: False

以上是 python如果语句失败 的全部内容, 来源链接: utcz.com/qa/260692.html

回到顶部