1

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.

10
  • Use endswith and startswith methods of the string to do the checks. Commented Feb 27, 2016 at 2:05
  • How do I do this? I really have no experience with this program, I'm in an entry level class and we haven't been taught this in class as ridiculous as it sounds Commented Feb 27, 2016 at 2:06
  • Please edit your title to reflect the problem you have. This question is useless to anybody else if it cannot be found, and nobody is going to search for "Need help on an easy, but problematic homework issue". Commented Feb 27, 2016 at 2:06
  • 2
    Don't say 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 like if inputURL.startswith("http") and any(inputURL.startswith(ending) for ending in (".com", ".net", ".org")): Commented Feb 27, 2016 at 2:07
  • 1
    @Marc: "is there any reason as to why I was getting a true result using "www.google.com" with my previous code" This is information you should have posted in the question from the very beginning. Commented Feb 27, 2016 at 2:35

1 Answer 1

1

This is definitely one of the most common mistakes beginner make, the first thing you need to understand is that all objects can be used in truth testing:

if "http":
    print("it was true!!")

then you can consider the order of execution of the conditional you wrote:

 if "http" and ".com" in inputURL

is equivalent to this:

 if ("http") and (".com" in inputURL)

so because "http" always evaluates as True the second part is the only thing that really contributed (thats why www.google.com works) what you want instead is:

 if ("http" in inputURL) and (".com" in inputURL):

although the startswith and endswith methods are definitely preferable since it checks only at the beginning and end:

 if inputURL.startswith("http") and inputURL.endswith(".com")

you can see the documentation on these methods (and everything else in python) with the help function:

 help(str.startswith)

Help on method_descriptor:

startswith(...) S.startswith(prefix[, start[, end]]) -> bool

Return True if S starts with the specified prefix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. prefix can also be a tuple of strings to try.

Even for me using help is always useful, I only just learned that startswith and endswith can take a tuple of strings to try:

 S.startswith(("a","b","c"))

this would return True if the string S starts with either "a" or "b" or "c", using this you can probably write your function in one line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.