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

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

(Python) 驗證和輸入適用于二進制,但不適用于其他基數(shù)系統(tǒng)

(Python) 驗證和輸入適用于二進制,但不適用于其他基數(shù)系統(tǒng)

喵喵時光機 2023-02-07 13:42:22
我是大學(xué) IT 的一年級學(xué)生,我們的教授要求我們制作一個數(shù)字系統(tǒng)轉(zhuǎn)換器。我們不允許使用任何會自動轉(zhuǎn)換為數(shù)字系統(tǒng)的方法。我使用此代碼來驗證二進制輸入: def base2():    s = {'0','1'}    y = input('Allowed numbers are "0-1", Enter your whole number: ')    val = set(y)        while not (s == val or val == {'0'} or val == {'1'}):        y = input('Allowed numbers are "0-1", Enter your whole number: ')        val = set(y)    return y對于 base-2 以上的其他數(shù)字系統(tǒng),我只是添加了缺少的驗證。例如在 base-3 中:def base3():    s = {'0','1','2'}    y = input('Allowed numbers are "0-2", Enter your whole number: ')    val = set(y)        while not (s == val or val == {'0'} or val == {'1'} or val == {'2'}):        y = input('Allowed numbers are "0-2", Enter your whole number: ')        val = set(y)    return y我只是添加到每個驗證的結(jié)尾。但出于某種原因,它要求驗證所有數(shù)字或允許的數(shù)字之一的重復(fù)。輸入輸出:Allowed numbers are "0-2", Enter your whole number: 20Allowed numbers are "0-2", Enter your whole number: 111['1', '1', '1']The decimal value of    " 111 "   base- 3  is 13Allowed numbers are "0-2", Enter your whole number: 211Allowed numbers are "0-2", Enter your whole number: 210['2', '1', '0']The decimal value of    " 210 "   base- 3  is 21將不勝感激任何幫助 <3
查看完整描述

1 回答

?
慕蓋茨4494581

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

使用以下條件:

while any(x not in s for x in val):

代替:

while not (s == val or val == {'0'} or val == {'1'} or val == {'2'})

當(dāng)您轉(zhuǎn)換為集合時,我們無法保證它的順序,因為集合本身沒有排序。因此,檢查s == val可能并不總是如您所想的那樣有效。此外,用戶可以輸入任意數(shù)字組合。


查看完整回答
反對 回復(fù) 2023-02-07
  • 1 回答
  • 0 關(guān)注
  • 84 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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