Skip to main content
Commonmark migration
Source Link

#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 as names, 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 pass names[-1:5] to random.choice, rather than names[0:5]. Thus, we always choose the last name in the list.

#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 as names, 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 pass names[-1:5] to random.choice, rather than names[0:5]. Thus, we always choose the last name in the list.

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 as names, 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 pass names[-1:5] to random.choice, rather than names[0:5]. Thus, we always choose the last name in the list.

added 501 characters in body
Source Link
daviewales
  • 281
  • 1
  • 6

#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 as names, 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 pass names[-1:5] to random.choice, rather than names[0:5]. Thus, we always choose the last name in the list.

#Python

#!/usr/bin/env python3

import random

names = ['John', 'Jeff', 'Emma', 'Steve', 'Julie']

start = names.index(names[-1]) - len(names)
end = len(names)

print(random.choice(names[start:end]))

#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 as names, 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 pass names[-1:5] to random.choice, rather than names[0:5]. Thus, we always choose the last name in the list.

Source Link
daviewales
  • 281
  • 1
  • 6

#Python

#!/usr/bin/env python3

import random

names = ['John', 'Jeff', 'Emma', 'Steve', 'Julie']

start = names.index(names[-1]) - len(names)
end = len(names)

print(random.choice(names[start:end]))