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

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

如何判斷一個(gè)數(shù)是否是二進(jìn)制數(shù)?

如何判斷一個(gè)數(shù)是否是二進(jìn)制數(shù)?

RISEBY 2023-12-12 09:55:25
my_l1:str = "0101011"我執(zhí)行這段代碼:for character in my_l1:    if character != '0' and character != '1':       print (f"{my_l1} is not a binary number")    else:       print(f"{my_l1} is a binary number")我得到這個(gè)輸出:0101011 is a binary number0101011 is a binary number0101011 is a binary number0101011 is a binary number0101011 is a binary number0101011 is a binary number0101011 is a binary number如何獲得像這樣的單行輸出?0101011 is a binary number.
查看完整描述

3 回答

?
拉莫斯之舞

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

您可以添加break和for..else。break將結(jié)束循環(huán)。


my_l1:str = "0101011"


for character in my_l1:

    if character != '0' and character != '1':

       print (f"{my_l1} is not a binary number")

       break

else:

    print (f"{my_l1} is a binary number")

印刷:


  0101011 is a binary number.

您可以使用以下方法重寫(xiě)代碼all():


my_l1:str = "0101011"


if all(ch=='0' or ch=='1' for ch in my_l1):

    print (f"{my_l1} is a binary number")

else:

    print (f"{my_l1} is not a binary number")



查看完整回答
反對(duì) 回復(fù) 2023-12-12
?
慕標(biāo)琳琳

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

 my_l1:str = "0101011"

 isBinary=True ## We Assume it's True (i.e, the number is a binary) until we prove to 

               ## the contrary, so this boolean variable is used to check (if binary 

               ## or not)

 for character in my_l1:

        if character != '0' and character != '1':

                  sBinary=False ## once we find a number in the list my_l1 different 

                                ## than "0" and "1"  it is not necessary to continue 

                                ## looping through the list, because the number is not 

                                ## a binary anymore, so we break in order to consume 

                                ## time

                  break

 

  if isBinary: ## i.e, if isBinary==True then do :

          print (f"{my_l1} is a binary number")

  else:

          print(f"{my_l1} is not a binary number")

 


查看完整回答
反對(duì) 回復(fù) 2023-12-12
?
繁花如伊

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

嘗試這個(gè):


def isBinary(num: str):

    for i in num:

        if i not in ["0","1"]:

            return False

    return True

if isBinary("000101") == True:

    print("000101 is binary")


查看完整回答
反對(duì) 回復(fù) 2023-12-12
  • 3 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

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