I'm new to Python. I'm stumped on this last question and I don't know what I'm doing wrong. The question is:
Define a function that determines if an input string is in the form of a web address that begins with "http" and ends in either ".com" ".net" or ".org". If the input string ends with one of these suffixes and starts with "http" the function will return True, otherwise it will return False.
def isCommonWebAddressFormat(inputURL):
This is what I currently have in my Python code, but it's turning out wrong results when I test it:
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
When I call the function with "www.google.com", the result is True, even though it should be False.
endswithandstartswithmethods of the string to do the checks.if "http" and ".com" in inputURL. You could put parentheses around"http" and ".com", and you would get the same result. You aren't checking them separately. Do something more likeif inputURL.startswith("http") and any(inputURL.startswith(ending) for ending in (".com", ".net", ".org")):