我是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'方法来执行检查。 –
我该怎么做?我真的没有这个计划的经验,我在入门级课程,我们没有在课堂上教过这个,因为它听起来很荒谬 –
请[编辑]你的头衔,以反映你的问题。如果无法找到这个问题,这个问题对任何其他人都没用,没有人会去搜索“在一个简单但有问题的作业问题上需要帮助”。 –