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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將break語(yǔ)句從函數(shù)發(fā)送到while循環(huán)?

如何將break語(yǔ)句從函數(shù)發(fā)送到while循環(huán)?

慕標(biāo)琳琳 2021-09-11 17:41:21
我試圖反復(fù)要求用戶輸入一個(gè)字符串。如果該字符串是“bye”,則程序應(yīng)返回“Bye”并終止。我不知道如何讓決定函數(shù)告訴 while 循環(huán)是時(shí)候終止了。def decide(greeting):    if greeting == "hi":        return "Hello"    elif greeting == "bye":        return "Bye"x = input("Insert here: ")while True:    print(decide(x))    x = input("Insert here: ")編輯:評(píng)論中的人說(shuō)在 while 循環(huán)中使用條件來(lái)檢查返回值。我不能這樣做,因?yàn)閷?shí)際上返回的值"Bye"存儲(chǔ)在局部變量中。這兩個(gè)函數(shù)實(shí)際上在一個(gè)類中,我更喜歡在條件語(yǔ)句上保持 while 循環(huán)較短。
查看完整描述

2 回答

?
守候你守候我

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以在函數(shù)中進(jìn)行打印并在 while 循環(huán)中檢查其輸出:


def decide(greeting):

    if greeting == "bye":

        print("Bye")

        return False  # only break on "bye";

    elif greeting == "hi":

        print("Hello")

    return True


while True:

    x = input("Insert here: ")

    if not decide(x):

        break

基于澄清的問(wèn)題進(jìn)行編輯(在您的函數(shù)中沒(méi)有打?。?。您的函數(shù)可以有多個(gè)輸出,例如:


def decide(greeting):

    if greeting == "bye":

        return "Bye", False  # return reply and status;

    elif greeting == "hi":

        return "Hello", True

    else:

        return greeting, True  # default case;


while True:

    x = input("Insert here: ")

    reply, status = decide(x)

    print(reply)

    if not status:

        break


查看完整回答
反對(duì) 回復(fù) 2021-09-11
?
明月笑刀無(wú)情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

你可以試試這個(gè):


def decide(greeting):

    if greeting == "hi":

         return "Hello"

    elif greeting == "bye":

        return "Bye"


x = input("Insert here: ")


while True:

    n = (decide(x))

    print(n)


    if(n == "Bye"):

        break


    x = input("Insert here: ")


查看完整回答
反對(duì) 回復(fù) 2021-09-11
  • 2 回答
  • 0 關(guān)注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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