python怎么返回true_在python中返回1而不是true

我试图在python中返回一个而不是true.

我正在处理的代码是:

delimiters = ( '()', '[]', '{}', "''", '""' )

esc = '\\'

def is_balanced(s, delimiters=delimiters, esc=esc):

stack = []

opening = tuple(str[0] for str in delimiters)

closing = tuple(str[1] for str in delimiters)

for i, c in enumerate(s):

if len(stack) and stack[-1] == -1:

stack.pop()

elif c in esc:

stack.append(-1)

elif c in opening and (not len(stack) or opening[stack[-1]] != closing[stack[-1]]):

stack.append(opening.index(c))

elif c in closing:

if len(stack) == 0 or closing.index(c) != stack[-1]:

return False

stack.pop()

return len(stack) == 0

num_cases = raw_input()

num_cases = int(num_cases)

for num in range(num_cases):

s = raw_input()

print is_balanced(s)

它基本上检查键入的字符串是否平衡.如果平衡,应返回1,如果不是0.

我试过这个:

1

Test string

True

它返回true.我希望它返回1.我该怎么办?

解决方法:

或者,您可以将布尔值转换为int:

>>>myBoolean = True

>>>int(myBoolean)

1

>>>myBoolean = False

>>>int(myBoolean)

0

标签:python,python-2-7,boolean

来源: https://codeday.me/bug/20190711/1434819.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值