string="I can't wait for Christmas!!"
other_symbols='''!()-[]{};:\<>/?#$%^&*_~'''
if other_symbols in string:
print('wrong')
else:
print('right')
它應該忽略除@
以外的所有符號,並且我的輸出應該是錯誤的,但我一直在正確。檢查一個句子是否包含符號
string="I can't wait for Christmas!!"
other_symbols='''!()-[]{};:\<>/?#$%^&*_~'''
if other_symbols in string:
print('wrong')
else:
print('right')
它應該忽略除@
以外的所有符號,並且我的輸出應該是錯誤的,但我一直在正確。檢查一個句子是否包含符號
使用any
來檢查other_symbols
中的任何符號是否在cavs
中。最好讓cavs
一套太爲O(1)
成員測試:
sc = set(cavs)
if any(i in sc for i in other_symbols):
print("wrong")
else:
print("right")
或者,如@Steven Rumbalski在評論中指出,你可以扭轉這種局面,使other_symbols
一套,做:
if any(c in other_symbols for c in cavs):
# rest similar
這將是如果有多個句子進行測試,這是一個改進,因爲您不必爲每個句子創建一個集合。
謝謝,但我不能使用任何或設置在我的代碼,因爲我們從來沒有在我的課上了解它的任何東西 – CAVS
然後只是不要把它包裝在'set' @CAVS,只是做'任何(我在cavs爲我other_symbols)'。 :) –
'如果有(符號在其他_符號中的符號)'。您的支票會檢查是否是''''!() - [] {};:\ <> /?#$%^&* _〜''''是''LET'S GO !! @cavs''的子字符串顯然是錯誤的。 –