【解決方法】リスト内のすべての要素が別の項目と等しいかどうかの条件チェックを作成する方法


これより短い条件を書きたい

Python
y = (website[-4:]!=".com" and website[-4:]!=".net" and website[-4:]!=".org"
    and website[-4:]!=".gov" and website[-4:]!=".edu")

私が試したこと:

Python
website = str(input("Enter your URL: "))
protocol = "http://www."
site = [".com" , ".net" , ".org" , ".gov" , ".edu"]

y = (website[-4:]!=".com" and website[-4:]!=".net" and website[-4:]!=".org"
 and website[-4:]!=".gov" and website[-4:]!=".edu")

# how to replace y with shorter condition

if website[0:11] != protocol:
    print("invaild")
elif website[0:11] == protocol:
    for i in site:       
        if website[-4:]==i:  
            print("http")
            print("www")
            print(website[11:-4])
            print(website[-3:])
        elif y == True:
            print("invaild")    
            break

解決策 1

見積もり:

これより短い条件を書きたい

Python
y = (ウェブサイト[-4:]!=”.com” とウェブサイト[-4:]!=”.net” とウェブサイト[-4:]!=”.org”
とウェブサイト[-4:]!=”.gov” とウェブサイト[-4:]!=”.edu”)

Python
y =  not (website[-4:] in site)

コメント

タイトルとURLをコピーしました