Question: I'm trying to figure out how to make a python program that highlights the position/positions of a certain inputed word in a sentence and lists what place that word is. For example if the sentence was: "The fat cat sat on the mat"
then the position for the word fat would be Number 2.

Heres what Ive got so far:

```
varSentence = ("The fat cat sat on the mat")

print (varSentence)

varWord = input("Enter word ")

varSplit = varSentence.split()

if varWord in varSplit:
    print ("Found word")
else:
    print ("Word not found")

```

Answer: 
It may depend of the scenario and your coding style. Jon Skeet is absolutely right about why they should be separated when you're using `yield` to create iterators.

BTW, I thought it could be interesting adding my two cents here: same code using *Code Contracts* (i.e. *design by contract*) behaves in a different way.

Pre-conditions aren't part of the iterator block, thus, the following code will throw a contract exception immediately if the whole pre-conditions aren't met:

```
public static class Test
{
    public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
    {
        Contract.Requires(source != null);
        Contract.Requires(predicate != null);

        foreach (T item in source)
        {
            if (predicate(item))
            {
                yield return item;
            }
        }
    }
}

// This throws a contract exception directly, no need of 
// enumerating the returned enumerable
Test.Where<string>(null, null);

```
A method using `yield return` looks very nice and simple, but if you examine the compiled code you will notice it gets quite complicated.

The compiler generates a new class for you with state machine logic to support the enumeration. For the 2nd `Where` method, it's around 160 lines of code after decompilation. The actual `Where` method is compiled to

```
[IteratorStateMachine(typeof(IterarorTest.<Where>d__0<>))]
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
    IterarorTest.<Where>d__0<T> expr_07 = new IterarorTest.<Where>d__0<T>(-2);
    expr_07.<>3__source = source;
    expr_07.<>3__predicate = predicate;
    return expr_07;
}

```

As you can see, no arguments are checked in this method. It just returns a new iterator.

The arguments are checked in the auto-generated class' `MoveNext` method (the code is a bit too long to post here).

On the other hand, if you move the `yield return` to another method, the arguments are checked immediately when you call the `Where` method - which is the expected behavior here.

**Edit**

As [noticed by Matias Fidemraizer](https://stackoverflow.com/a/34187364/4588915), code contracts also solve the problem - the contract checks are inserted in the `Where` method

```
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
    __ContractsRuntime.Requires(source != null, null, "source != null");
    __ContractsRuntime.Requires(predicate != null, null, "predicate != null");
    IterarorTest.<Where>d__0<T> expr_27 = new IterarorTest.<Where>d__0<T>(-2);
    expr_27.<>3__source = source;
    expr_27.<>3__predicate = predicate;
    return expr_27;
}

```