Reputation: 391
I created a game to guess US States and Capitals. It works in Jupyter Notebook. When I save the code to game.py and run it in Windows CMD, my code does not loop 10 times, but just one time, then the CMD will close automatically. Any reason why?
Also, I used random.choice function to randomly select 10 states, how do I avoid duplicate? I want 10 separate states each time I run my code.
Thank you!
import random
capital_dic={
'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',
'Illinios': 'Springfield',
'Indiana': 'Indianapolis',
'Iowa': 'Des Monies',
'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',
'Neveda': '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',
'Rhoda Island': 'Providence',
'South Carolina': 'Columbia',
'South Dakoda': 'Pierre',
'Tennessee': 'Nashville',
'Texas': 'Austin',
'Utah': 'Salt Lake City',
'Vermont': 'Montpelier',
'Virginia': 'Richmond',
'Washington': 'Olympia',
'West Virginia': 'Charleston',
'Wisconsin': 'Madison',
'Wyoming': 'Cheyenne'
} # create a dictionary, key is the state and value is the capital
States=list(capital_dic.keys())
print ('Let\'s learn US States and Capitals. 10 rounds. Enter exit to quit the game.')
point=0 # this is the score
for i in range(10):
state=random.choice(States) # randomly select 10 states, how do I avoid duplicate?
capital = capital_dic[state]
user_guess = input('what is the capital of %s?'%state )
if user_guess.lower() == 'exit': #if a user type in exit, the game exits
break
elif user_guess.title() == capital:
point+=1
print('Correct! Your score is %d' %point)
else:
print('Incorrect. The capital of {} is {}.'.format(state,capital))
print('We are done. Your final score is %d, thank you.' %point)
Upvotes: 2
Views: 24636
Reputation: 1438
Your problem with looping was that you are using python2
in your windows cmd. input
function is little bit different from python3
version, so try to use raw_input
function if you want to run this code with python2
:
user_guess = raw_input('what is the capital of %s?' % state)
see this answer for details
Upvotes: 0
Reputation: 82785
You can use random.sample
to get unique set of random names.
Ex:
States=list(capital_dic.keys())
print random.sample(States, 10)
Output:
['Michigan', 'Alabama', 'Virginia', 'Massachusetts', 'Rhoda Island', 'Kentucky', 'Louisiana', 'Wyoming', 'Arizona', 'Vermont']
Snippet Update:
for state in random.sample(States, 10):
capital = capital_dic[state]
Upvotes: 1