cisconethead cisconethead - 7 months ago 101
Python Question

Python random.choice method without repeats?

I'm trying to improve this code which asks the user to say what the state capital is when given a state, but I've noticed that sometimes it will repeat a state and ask it twice.

I tried using random.sample instead, but I got an error "TypeError: Unhashable type: 'list'. Here is the code that works (but repeats) with the random.sample commented out:

capitals_dict = {
'Alabama': 'Montgomery',
'Alaska': 'Juneau',
'Arizona': 'Phoenix',
'Arkansas': 'Little Rock',
'California': 'Sacramento',
'Colorado': 'Denver',
'Connecticut': 'Hartford',
'Delaware': 'Dover',
'Florida': 'Tallahassee',
'Georgia': 'Atlanta',
'Hawaii': 'Honolulu',
'Idaho': 'Boise',
'Illinois': 'Springfield',
'Indiana': 'Indianapolis',
'Iowa': 'Des Moines',
'Kansas': 'Topeka',
'Kentucky': 'Frankfort',
'Louisiana': 'Baton Rouge',
'Maine': 'Augusta',
'Maryland': 'Annapolis',
'Massachusetts': 'Boston',
'Michigan': 'Lansing',
'Minnesota': 'St. Paul',
'Mississippi': 'Jackson',
'Missouri': 'Jefferson City',
'Montana': 'Helena',
'Nebraska': 'Lincoln',
'Nevada': 'Carson City',
'New Hampshire': 'Concord',
'New Jersey': 'Trenton',
'New Mexico': 'Santa Fe',
'New York': 'Albany',
'North Carolina': 'Raleigh',
'North Dakota': 'Bismarck',
'Ohio': 'Columbus',
'Oklahoma': 'Oklahoma City',
'Oregon': 'Salem',
'Pennsylvania': 'Harrisburg',
'Rhode Island': 'Providence',
'South Carolina': 'Columbia',
'South Dakota': 'Pierre',
'Tennessee': 'Nashville',
'Texas': 'Austin',
'Utah': 'Salt Lake City',
'Vermont': 'Montpelier',
'Virginia': 'Richmond',
'Washington': 'Olympia',
'West Virginia': 'Charleston',
'Wisconsin': 'Madison',
'Wyoming': 'Cheyenne',
}

import random

states = list(capitals_dict.keys())
for i in [1, 2, 3, 4, 5]:
state = random.choice(states)
#state = random.sample(states, 5)
capital = capitals_dict[state]
capital_guess = input('What is the capital of ' + state + '?')

if capital_guess == capital:
print('Correct! Nice job!')
else:
print('Incorrect. The Capital of ' + state + ' is ' + capital + '.')

print('All done.')


I also tried just using the dictionary name capitals_dict like this:

random.sample(capitals_dict, 5)


but I got a different error then found out that I can't use dictionaries like that.

Answer

You can create a list of all keys in the dictionary by passing the dictionary to the list() function first, then sample from that list:

sample = random.sample(list(capitals_dict), 5)

You can also pass in the dict.keys() dictionary view:

sample = random.sample(capitals_dict.keys(), 5)

but internally random.sample() will just convert that to a sequence too (a tuple()) so using list() is actually more efficient here.

The exception you encountered actually tells you this:

>>> random.sample(capitals_dict, 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python3.4/random.py", line 311, in sample
    raise TypeError("Population must be a sequence or set.  For dicts, use list(d).")
TypeError: Population must be a sequence or set.  For dicts, use list(d).
#                                                 ^^^^^^^^^^^^^^^^^^^^^^^

Demo:

>>> import random
>>> capitals_dict = {
...     'Alabama': 'Montgomery',
...     'Alaska': 'Juneau',
...     'Arizona': 'Phoenix',
...     'Arkansas': 'Little Rock',
...     'California': 'Sacramento',
...     # ... elided ...
... }
>>> 
>>> random.sample(list(capitals_dict), 5)
['Maryland', 'Mississippi', 'Wisconsin', 'Texas', 'West Virginia']

To incorporate that into your code:

import random

for state in random.sample(list(capitals_dict), 5):
    capital = capitals_dict[state]
    capital_guess = input('What is the capital of {}?'.format(state))

    if capital_guess == capital:
        print('Correct! Nice job!')
    else:
        print('Incorrect. The Capital of {} is {}.'.format(state, capital))

I also replaced your string concatenations with str.format() calls to put values into string templates instead.