#Python
Python
#!/usr/bin/env python3
import random
# Create list of names.
names = ['John', 'Jeff', 'Emma', 'Steve', 'Julie']
# Define start and end of range, just in case we want to change it in the future.
start = names.index(names[-1]) - len(names)
end = len(names)
# Randomly choose name from list, using range specified above.
print(random.choice(names[start:end]))
names[start:end]
should be the same asnames
, but allows us to easily change the range in the future. However, instead of starting at 0, as we should, we sneakily start at -1. So, we actually passnames[-1:5]
torandom.choice
, rather thannames[0:5]
. Thus, we always choose the last name in the list.