Reputation: 389
I'm new to python, and I'm building a game to teach myself python. This game will have a number of lessons, with questions and answers; the user will gain and lose points depending on the validity of their answers.
I'm using dictionaries to store the questions and answers that will be asked in each lesson.
I want to display and check the keys and values of the dictionary only at specific points (e.g. after the user enters a command). To do this, I imagined that I could create functions containing the dictionaries and then pass them to a main function when needed.
But when I run the code below, I get the following error: AttributeError: 'function' object has no attribute 'iteritems'
So I have two questions:
Here's my code so far. Any advice whatsoever would be much appreciated!
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)
and the code that works:
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)
Upvotes: 0
Views: 3846
Reputation: 2518
You passed a function that returns a dictionary so you should call the function first to get the dictionary. So you may modify your code so that main accepts dictionary (the code actually expects a dictionary):
main(lesson1())
If you really would like to pass a function then you should modify you main
to execute function first to get the dictionary:
def main(lessonFunc):
global points
lesson = lessonFunc()
#get key:value pair from dictionary
for k, v in lesson.iteritems():
but the first option is probably better. You could also pack a lesson into an object.
Upvotes: 1
Reputation: 2881
You're calling main() with the lesson1 function and not the result of the lesson1 function (which is a directory).
You should write:
main(lesson1())
By the way, lesson1 must also return the created directory for this to work:
def lesson1():
#create a dictionary
mydict = {
"q1":"a1",
"q2":"a2"
}
return mydict
Upvotes: 1