我有50个州和首都。游戏是用户以随机顺序被赋予50个状态的地方,并且必须为每个州输入正确的资本。
我想创建一个包含所有州和大写字母的字典,但我不知道如果用户输入正确的资金,如何检查程序。
# Game Start
import random
states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']
random.shuffle(states)
for state in states:
answer = raw_input ("%s" % state)
# DICTIONARY
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':Saint_Paul, 'Mississippi':Jackson, 'Missouri':Jefferson_City, 'Montana':Helena, 'Nebraska':Lincoln, 'Nevada':Carson_City, 'New Hampshire':Concord, 'New Jersey':Trenton, 'New Mexico':Sante_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}
如何检查答案是否在字典中?另外,python告诉我名字'Dict'没有定义
答案 0 :(得分:2)
您可以这样做:
import random
capitals = {'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':'Saint_Paul', 'Mississippi':'Jackson', 'Missouri':'Jefferson_City', 'Montana':'Helena', 'Nebraska':'Lincoln', 'Nevada':'Carson_City', 'New Hampshire':'Concord', 'New Jersey':'Trenton', 'New Mexico':'Sante_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'}
states = list(capitals.keys())
random.shuffle(states)
for state in states:
answer = raw_input("%s" % state)
if answer == capitals[state]:
print 'Correct!'
else:
print 'Wrong!'
答案 1 :(得分:0)
#Game Start
import random
states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']
###DICTIONARY: Should be defined before you use it
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':Saint_Paul, 'Mississippi':Jackson, 'Missouri':Jefferson_City, 'Montana':Helena, 'Nebraska':Lincoln, 'Nevada':Carson_City, 'New Hampshire':Concord, 'New Jersey':Trenton, 'New Mexico':Sante_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}
random.shuffle(states)
for state in states:
answer = raw_input ("%s" % state)
if Dict[state] == answer:
pass # Do something in case of right answer
else:
pass # what do you want in case of wrong answer
答案 2 :(得分:0)
我已经测试了这个,它运行得很好。但它在Python 3中,所以你需要做一些小的调整。
import random
states =['Alabama', 'Alaska', 'Arizona']
random.shuffle(states)
# dictionary need to be stated beforehand to check the input
# use '' for the state capital
state_dict = {'Alabama':'Montgomery', 'Alaska':'Juneau', 'Arizona':'Phoenix'}
for state in states:
answer = input("%s " %state)
if answer==(state_dict[state]):
print ("Correct")
else :
print ("False")
答案 3 :(得分:0)
为避免双重簿记,您可以在字典项目上使用 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/colorPrimaryDark"
android:text="text"
android:textSize="22dp" />
</RelativeLayout>
</LinearLayout>
。
它也更加pythonic,因为你直接在键和值上循环,而不是在键上循环然后查找:
random.sample