[Courses] [python] Lesson 6: Functions and dictionaries
Prana Peddi
prana.lists at gmail.com
Mon Jul 25 19:02:27 UTC 2011
My answers below:
Thanks,
Prana
===================== Homework ===========================
>
> 1. Write a function that takes a dictionary as argument, chooses a random
> key from that dictionary, and returns the key and its value as a tuple.
>
> In other words, if you used that urls dictionary I defined above,
> you would pass urls as the argument to the function, and the function
> might return something like (LinuxChix", "http://linuxchix.org").
>
> #! /usr/bin/python
import random
quest = {"What are the colors of the US flag?" : "Red, white, blue",
"Who is the President of the United States?" : "Barack Obama",
"Who is the Vice President of the United States?" : "Joe Biden",
"Who wrote the Declaration of Independence?" : "Thomas JEFFERSON",
"Who was the first President of the United States?" : "GEorge
Washington",
}
def getrandomsample(dictname):
key = random.choice(dictname.keys())
return (key, dictname[key])
tup = getrandomsample(quest)
print tup[0], tup[1]
2. Use the function you just defined to make a flashcard program.
> The flashcards can be on any subject you want (or a mix of
> subjects). Make a dictionary of questions and answers -- the keys
> are the questions, the answers are the values. Then use your
> function to pick a random question/answer pair, print the question
> and wait for the user to hit return. Then print the answer.
> (The user can keep track of whether she got it right.)
>
> By the way, if you ever need to use strings with non-ASCII characters
> -- like if you're making flashcards for a language besides English --
> you can specify an encoding with a comment near the top of your program
> (e.g. right after the shebang line, if any) like this:
> # -*- coding: utf-8 -*-
>
#! /usr/bin/python
import random
quest = {"What are the colors of the US flag?" : "Red, white, blue",
"Who is the President of the United States?" : "Barack Obama",
"Who is the Vice President of the United States?" : "Joe Biden",
"Who wrote the Declaration of Independence?" : "Thomas JEFFERSON",
"Who was the first President of the United States?" : "GEorge
Washington",
}
def getrandomsample(dictname):
key = random.choice(dictname.keys())
return (key, dictname[key])
while(1):
tup = getrandomsample(quest)
t = raw_input("{0}\n".format(tup[0]))
if (t == "q"):
print "Exiting"
break
else:
print tup[1]
3. Change your flashcard program so that the user has to type the answer,
> and you compare it against the right answer and keep track of how
> many were answered right or wrong.
>
> Note: you may find this is kind of a pain, because if you make any
> typos or add extra spaces or anything you don't get credit for a
> right answer. If you find this to be a problem, do you have any ideas
> for ways you could make it more flexible?
>
#! /usr/bin/python
import random
quest = {"What are the colors of the US flag?" : "Red, white, blue",
"Who is the President of the United States?" : "Barack Obama",
"Who is the Vice President of the United States?" : "Joe Biden",
"Who wrote the Declaration of Independence?" : "Thomas JEFFERSON",
"Who was the first President of the United States?" : "GEorge
Washington",
}
def getrandomsample(dictname):
key = random.choice(dictname.keys())
return (key, dictname[key])
def checkresult(str1, str2):
flag = True
str1 = str1.strip().lower().split()
str2 = str2.strip().lower().split()
if(len(str1) == len(str2)):
for i in range(len(str1)):
if str1[i] != str2[i]:
flag = False
break
else:
flag = False
return flag
rt = 0
wr = 0
while(1):
flag = True
tup = getrandomsample(quest)
t = raw_input("{0}\n".format(tup[0]))
if (t == "q"):
print "Exiting"
break
else:
if checkresult(tup[1], t) :
print "That's right!"
rt += 1
else:
print "Sorry, Wrong. The Answer is
{0}.".format(tup[1])
wr += 1
print "Your score: {0}/{1}".format(rt, rt+wr)
More information about the Courses
mailing list