加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在python中返回1而不是true

发布时间:2020-12-20 13:05:44 所属栏目:Python 来源:网络整理
导读:我试图在 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]
我试图在 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读