Python:如何将包含字典的函数传递给另一个函数?

时间:2012-05-10 07:41:30

标签: python function dictionary arguments

我是python的新手,我正在建立一个自学python的游戏。这个游戏将有一些课程,有问题和答案;用户将根据答案的有效性获得和失去分数。

我正在使用词典来存储每节课中会提出的问题和答案。

我想仅在特定点显示和检查字典的键和值(例如,在用户输入命令之后)。为此,我想象我可以创建包含字典的函数,然后在需要时将它们传递给main函数。

但是当我运行下面的代码时,我收到以下错误:AttributeError:'function'对象没有属性'iteritems'

所以我有两个问题:

  1. 我尝试从函数中删除字典,它可以工作 就好了。是否有任何方式(或理由)使其在内部工作 一个功能?
  2. 是否可以只使用一个字典并在某些点检查其中一部分的键和值?
  3. 到目前为止,这是我的代码。任何建议都将不胜感激!

    points = 10 # user begins game with 10 pts 
    
    def point_system(): 
        global points
    
        #help user track points
        if 5 >= points: 
            print "Careful. You have %d points left." % points 
        elif points == 0: 
            dead("You've lost all your points. Please start over.")
        else:
            print "Good job. Spend your points wisely." 
    
    def lesson1(): 
        #create a dictionary 
        mydict = {
        "q1":"a1", 
        "q2":"a2"
        }
    
    return mydict
    
    def main(lesson):
        global points
    
        #get key:value pair from dictionary
        for k, v in lesson.iteritems():
            lesson.get(k,v) # Is the .get step necessary? It works perfectly well without it.
            print k
            user_answer = raw_input("What's your answer?: ") 
    
    #test if user_answer == value in dictionary, and award points accordingly 
            if user_answer == v:
                user_answer = True
                points += 1 #increase points by 1 
                print "Congrats, you gained a point! You now have %d points" % points 
                point_system()
            elif user_answer != v: 
                points -= 1 #decrease points by 1 
                print "Oops, you lost a point. You now have %d points" % points 
                point_system()
            else: 
                print "Something went wrong."
                point_system()
    
    
    main(lesson1) 
    

    以及有效的代码:

    points = 10 # user begins game with 10 pts 
    
    #create a dictionary 
    lesson1 = {
    "q1":"a1", 
    "q2":"a2"
    }
    
    def point_system(): 
        global points
    
        #help user track points
        if 5 >= points: 
        print "Careful. You have %d points left." % points 
        elif points == 0: 
        dead("You've lost all your points. Please start over.")
        else:
        print "Good job. Spend your points wisely." 
    
    def main(lesson):
        global points
    
        #get key:value pair from dictionary
        for k, v in lesson.iteritems():
            lesson.get(k,v) # Is the .get step necessary? It works perfectly well without it.
            print k
            user_answer = raw_input("What's your answer?: ") 
    
    #test if user_answer == value in dictionary, and award points accordingly 
            if user_answer == v:
                user_answer = True
                points += 1 #increase points by 1 
                print "Congrats, you gained a point! You now have %d points" % points 
                point_system()
            elif user_answer != v: 
                points -= 1 #decrease points by 1 
                print "Oops, you lost a point. You now have %d points" % points 
                point_system()
            else: 
                print "Something went wrong."
                point_system()
    
    
    main(lesson1) 
    

2 个答案:

答案 0 :(得分:1)

您使用lesson1函数调用main(),而不是使用lesson1函数(这是一个目录)的结果。

你应该写:

main(lesson1())

顺便说一下,lesson1还必须返回创建的目录才能使用:

def lesson1(): 
    #create a dictionary 
    mydict = {
        "q1":"a1", 
        "q2":"a2"
    }
    return mydict

答案 1 :(得分:1)

您传递了一个返回字典的函数,因此您应该首先调用该函数来获取字典。因此,您可以修改代码,以便main接受字典(代码实际上需要字典):

main(lesson1())

如果你真的想传递一个函数,那么你应该修改你main来先执行函数来获取字典:

def main(lessonFunc):
    global points
    lesson = lessonFunc()

    #get key:value pair from dictionary
    for k, v in lesson.iteritems():

但第一种选择可能更好。您还可以将课程打包到对象中。