第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創(chuàng)建一個函數(shù)并從中調(diào)用兩個不同的結(jié)果

創(chuàng)建一個函數(shù)并從中調(diào)用兩個不同的結(jié)果

皈依舞 2024-01-11 16:21:38
我正在嘗試檢查匹配方程的平衡。下面的代碼是我目前擁有的。#Creating the math equation the use check_html on.e = "10 - (3 + (2+1)*8)"  def check_html(html_string):   d = Stack()balanced = Truefor symbol in html_string: if symbol == "(":    d.push(symbol)elif symbol == ")":    if d.is_empty():        balanced = False    else:        d.pop()if not d.is_empty:balanced = Falsestr1 = "10 - (3 + (2+1)*8)"str2 = "10 - (3 + (2+1)*8))"print ("is the first valid?", check_html(str1))print ("is the second valid?", check_html(str2))print("Is it balanced? ", balanced) 這段代碼的輸出是is the first valid? Noneis the second valid? NoneIs it balanced?  True應(yīng)該說第一個是 TRUE,第二個是 FALSE。我現(xiàn)在做錯了什么。
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經(jīng)驗 獲得超1個贊

你必須使用html_string而不是e在線


for symbol in html_string:  # `html_string` instead` of `e`

就這樣。


順便說一句:最好在Stack()函數(shù)內(nèi)部創(chuàng)建和使用 - 因此當(dāng)您使用新字符串運行函數(shù)時,它將創(chuàng)建新的空堆棧。


編輯:完整功能


# --- functions ---


def check_html(html_string):

    d = Stack()


    balanced = True


    for symbol in html_string: 

        if symbol == "(":

            d.push(symbol)

        elif symbol == ")":

            if d.is_empty():

                balanced = False

            else:

               d.pop()


    if not d.is_empty(): # you forgot `()

        balanced = False


    print("Is it balanced? ", balanced)


# --- main ---


e = "10 - (3 + (2+1)*8)"

check_html(e)

check_html( e+')' )

如果你想check_html()在print()then 函數(shù)中使用,你應(yīng)該return balanced使用of print()


# --- functions ---


def check_html(html_string):

    d = Stack()


    balanced = True


    for symbol in html_string: 

        if symbol == "(":

            d.push(symbol)

        elif symbol == ")":

            if d.is_empty():

                balanced = False

            else:

               d.pop()


    if not d.is_empty(): # you forgot `()

        balanced = False


    return balanced


# --- main ---


e = "10 - (3 + (2+1)*8)"

print("Is it balanced? ", check_html(e) )

print("Is it balanced? ", check_html( e+')' ) )

編輯:帶有自己的類的完整工作示例Stack


# --- classes ---


class Stack:


    def __init__(self):

        self.data = []


    def push(self, item):

        self.data.append(item)


    def pop(self):

        return self.data.pop(-1)


    def is_empty(self):

        return len(self.data) == 0



# --- functions ---


def check_html(html_string):

    d = Stack()


    balanced = True


    for symbol in html_string: 

        if symbol == "(":

            d.push(symbol)

        elif symbol == ")":

            if d.is_empty():

                balanced = False

            else:

                d.pop()


    if not d.is_empty(): # you forgot `()

        balanced = False


    return balanced


# --- main ---


e = "10 - (3 + (2+1)*8)"

print("Is it balanced?", check_html(e) )


#print("Is it balanced?", check_html(e + ')') )

f = e + ')'

print("Is it balanced?", check_html(f) )


#print("Is it balanced?", check_html('('+e+')') )

f = '(' + e + ')'

print("Is it balanced?", check_html(f) )


#print("Is it balanced?", check_html('('+e) )

f = '(' + e

print("Is it balanced?", check_html(f) )


查看完整回答
反對 回復(fù) 2024-01-11
  • 1 回答
  • 0 關(guān)注
  • 136 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號