我是Python新手。我被困在最後一個問題上,我不知道我做錯了什麼。問題是:Python多個字符串比較不起作用
定義一個函數,用於確定輸入字符串是否以網址形式以「http」開頭並以「.com」「.net」或「.org」結尾。 。如果輸入字符串以這些後綴之一結尾並以「http」開頭,則該函數將返回True,否則它將返回False。
def isCommonWebAddressFormat(inputURL):
這是我目前在我的Python代碼,但它的轉向了錯誤的結果,當我測試一下:
def isCommonWebAddressFormat(inputURL):
#return True if inputURL starts with "http" and ends with ".com" ".net" or ".org" and returns False otherwise
outcome = ""
if "http" and ".com" in inputURL:
outcome = True
elif "http" and ".net" in inputURL:
outcome = True
elif "http" and ".org" in inputURL:
outcome = True
else:
outcome = False
return outcome
當我調用該函數與"www.google.com"
,結果是True
,儘管它應該是False
。
使用字符串的'endswith'和'startswith'方法來執行檢查。 –
我該怎麼做?我真的沒有這個計劃的經驗,我在入門級課程,我們沒有在課堂上教過這個,因爲它聽起來很荒謬 –
請[編輯]你的頭銜,以反映你的問題。如果無法找到這個問題,這個問題對任何其他人都沒用,沒有人會去搜索「在一個簡單但有問題的作業問題上需要幫助」。 –