Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用字典做测验?_Python_List_Dictionary - Fatal编程技术网

Python 用字典做测验?

Python 用字典做测验?,python,list,dictionary,Python,List,Dictionary,所以我需要创建一个测验,给用户一个州,他们必须给我首都。但出于某种原因,当我运行它时,它并没有像我希望的那样删除旧城市 代码 问题是,只有当答案正确时,您才从dict中删除状态,您需要无条件地删除它,请尝试以下代码: import random def main(): capitals={"Washington":"Olympia","Oregon":"Salem",\ "California":"Sacramento","Ohio":"Colum

所以我需要创建一个测验,给用户一个州,他们必须给我首都。但出于某种原因,当我运行它时,它并没有像我希望的那样删除旧城市

代码


问题是,只有当答案正确时,您才从dict中删除状态,您需要无条件地删除它,请尝试以下代码:

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

    wrong=[]

    print ("STATE TEST \n")

    incorrect_answers = False

    while len(capitals)>0:
        pick=random.choice(list(capitals.keys()))
        correct_answer=capitals.get(pick)
        print ("What is the capital city of",pick,"?")
        answer=raw_input("Your answer: ")
        if answer.lower()==correct_answer.lower():
            print ("That's Correct!\n")
        else:
            print ("That's Incorrect.")
            print ("The correct answer is",correct_answer)
            wrong.append(pick)
            incorrect_answers = True
    del capitals[pick]

    print ("You missed",len(wrong),"states.\n")


    if incorrect_answers:
        print ("Here are the ones that you may want to brush up on:\n")
        for each in wrong:
            print (each)
    else:
        print ("Perfect!")
main()

希望有帮助

定义“不删除我想要的旧城市”。当用户猜错时,您不会从dict中删除状态,用户可以稍后再猜。这就是你想要的吗?另外,
不正确答案的定义在哪里?您有什么证据表明它没有删除正确答案的选项?你看到了重复的选择吗?尝试将初始的
dict
减少到只有1或2个条目,以便于调试。你是说del capitals[pick]在success if->then中不起作用吗?我注意到在fail if->then中没有相同的代码,所以您应该明确地将其添加到中。这些值为我从字典中删除。一旦字典为空,就会出现一个错误-
不正确的答案没有定义。这很可能是故意的行为-如果你弄错了,它会回到问题库中。这个问题并不清楚人们想要的行为是什么。问题的关键是,它并没有像他想要的那样删除旧城市,它看起来已经足够清楚了
import random
def main():
    capitals={"Washington":"Olympia","Oregon":"Salem",\
                    "California":"Sacramento","Ohio":"Columbus",\
                    "Nebraska":"Lincoln","Colorado":"Denver",\
                    "Michigan":"Lansing","Massachusetts":"Boston",\
                    "Florida":"Tallahassee","Texas":"Austin",\
                    "Oklahoma":"Oklahoma City","Hawaii":"Honolulu",\
                    "Alaska":"Juneau","Utah":"Salt Lake City",\
                    "New Mexico":"Santa Fe","North Dakota":"Bismarck",\
                    "South Dakota":"Pierre","West Virginia":"Charleston",\
                    "Virginia":"Richmond","New Jersey":"Trenton",\
                    "Minnesota":"Saint Paul","Illinois":"Springfield",\
                    "Indiana":"Indianapolis","Kentucky":"Frankfort",\
                    "Tennessee":"Nashville","Georgia":"Atlanta",\
                    "Alabama":"Montgomery","Mississippi":"Jackson",\
                    "North Carolina":"Raleigh","South Carolina":"Columbia",\
                    "Maine":"Augusta","Vermont":"Montpelier",\
                    "New Hampshire":"Concord","Connecticut":"Hartford",\
                    "Rhode Island":"Providence","Wyoming":"Cheyenne",\
                    "Montana":"Helena","Kansas":"Topeka",\
                    "Iowa":"Des Moines","Pennsylvania":"Harrisburg",\
                    "Maryland":"Annapolis","Missouri":"Jefferson City",\
                    "Arizona":"Phoenix","Nevada":"Carson City",\
                    "New York":"Albany","Wisconsin":"Madison",\
                    "Delaware":"Dover","Idaho":"Boise",\
                    "Arkansas":"Little Rock","Louisiana":"Baton Rouge"}

    wrong=[]

    print ("STATE TEST \n")

    incorrect_answers = False

    while len(capitals)>0:
        pick=random.choice(list(capitals.keys()))
        correct_answer=capitals.get(pick)
        print ("What is the capital city of",pick,"?")
        answer=raw_input("Your answer: ")
        if answer.lower()==correct_answer.lower():
            print ("That's Correct!\n")
        else:
            print ("That's Incorrect.")
            print ("The correct answer is",correct_answer)
            wrong.append(pick)
            incorrect_answers = True
    del capitals[pick]

    print ("You missed",len(wrong),"states.\n")


    if incorrect_answers:
        print ("Here are the ones that you may want to brush up on:\n")
        for each in wrong:
            print (each)
    else:
        print ("Perfect!")
main()