Python:如何将包含字典的函数传递给另一个函数?
我是python新手,我正在构建一个游戏来自学python。这个游戏将有许多课程,包括问题和答案;用户将根据其答案的有效性获得和失去分数 我用字典来存储每节课要问的问题和答案 我只想在特定点(例如,在用户输入命令后)显示和检查字典的键和值。为此,我设想可以创建包含字典的函数,然后在需要时将它们传递给主函数 但是当我运行下面的代码时,我得到了以下错误:AttributeError:“function”对象没有属性“iteritems” 所以我有两个问题:Python:如何将包含字典的函数传递给另一个函数?,python,function,dictionary,arguments,Python,Function,Dictionary,Arguments,我是python新手,我正在构建一个游戏来自学python。这个游戏将有许多课程,包括问题和答案;用户将根据其答案的有效性获得和失去分数 我用字典来存储每节课要问的问题和答案 我只想在特定点(例如,在用户输入命令后)显示和检查字典的键和值。为此,我设想可以创建包含字典的函数,然后在需要时将它们传递给主函数 但是当我运行下面的代码时,我得到了以下错误:AttributeError:“function”对象没有属性“iteritems” 所以我有两个问题: 我尝试从一个函数中删除字典,结果成功了 那
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)
您使用lesson1函数调用main(),而不是lesson1函数(目录)的结果
你应该写:
main(lesson1())
顺便说一下,lesson1还必须返回创建的目录,以使其正常工作:
def lesson1():
#create a dictionary
mydict = {
"q1":"a1",
"q2":"a2"
}
return mydict
您传递了一个返回字典的函数,因此应该首先调用该函数以获取字典。因此,您可以修改代码,使main接受dictionary(代码实际上需要一个dictionary): 如果您确实希望传递函数,则应修改您的
main
以首先执行函数以获取字典:
main(lesson1())
def main(lessonFunc):
global points
lesson = lessonFunc()
#get key:value pair from dictionary
for k, v in lesson.iteritems():
但第一种选择可能更好。你也可以将一节课打包到一个对象中。我想你需要在这里尝试一下。只是好奇……你是javascript程序员吗?谢谢你的参考,@DrTyrsa@parselmouth,是的,我是从javascript开始的——为什么?@user1186742你的lesson1()函数有点像一个用于生成新对象的javascript工厂函数。我想我会问一下你的javascript经验,因为知道你来自javascript背景,我们这些同时了解javascript和python的人可以更好地向你展示python如何以不同的方式做某些事情,以及为什么。无论如何-欢迎来到python社区!Python是一种令人惊叹的语言,我希望您会发现您喜欢使用它,并且非常高效。将字典放在函数中是最佳做法,还是将其放在函数外也同样可以?谢谢您的解释。将课程打包到对象中有什么好处?使用此小代码可能没有必要。
课程
对象可以有比问题更多的东西。比如课名,一些关于文学的信息等等。这可能会使代码更加清晰。看见