Python-字典状态和资本博弈 [英] Python-Dictionary states and capital game

查看:69
本文介绍了Python-字典状态和资本博弈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个猜测美国和首都的游戏.它可以在Jupyter Notebook中使用.当我将代码保存到game.py并在Windows CMD中运行时,我的代码不会循环10次,而只会循环一次,那么CMD将自动关闭.有什么原因吗?

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?

此外,我使用random.choice函数随机选择10个状态,如何避免重复?每次运行代码时,我都希望有10个单独的状态.

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.

谢谢!

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)

推荐答案

循环问题是您在Windows cmd中使用 python2 . input 函数与 python3 版本稍有不同,因此,如果要与 python2 <一起运行此代码,请尝试使用 raw_input 函数./code>:

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)

请参见此答案以获取详细信息

这篇关于Python-字典状态和资本博弈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆