我是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)
以及有效的代码:
^{pr2}$