python - How to assign a variable name from a list then delete it? -


i want create function takes list of names entered user. if there number of players, randomly print out names until there no more names left. if there odd number, assign variable name last name entered, delete list. same number of players. want able use last name later on in program.

here's have far. giving me error reading,

"raise valueerror, "empty range randrange() (%d,%d, %d)" % (istart, istop, width)"

def randname(teamnames):     if len(teamnames) % 2 ==0:         randomname = random.randrange(0, len(teamnames))         return teamnames.pop(randomname)     else:         lastname = teamnames[-1]         teamnames.pop() 

from random import shuffle  def random_pairings(team_names):     # randomize order     team_names = list(team_names)     shuffle(team_names)     # remove 'odd man out'     leftover = team_names.pop() if len(team_names) % 2 else none     # pairings     trick = [iter(team_names)] * 2   # 2 pointers *same* iterator     pairs = list(zip(*trick))     return pairs, leftover 

which gives

>>> random_pairings("abcdefg") ([('g', 'c'), ('d', 'f'), ('b', 'e')], 'a') 

so can use like

tennis_players = ["dimitrov", "herbert", "sock", "tomic", "thiem"]  pairs, leftover = random_pairings(tennis_players) a,b in pairs:     print("{} vs {}".format(a, b)) if leftover:     print("{} sits round out.".format(leftover)) 

giving

tomic vs herbert sock vs dimitrov thiem sits round out. 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -