The last check, `G.number_of_selfloops()==0`, is useless: a connected graph with self-loops necessarily has more than n - 1 edges.

On a different note, try to format the code so that line length is at most 79 characters.