Created
December 17, 2012 21:31
-
-
Save yfwu/4322466 to your computer and use it in GitHub Desktop.
寄生蟲跑考資料庫建立與練習程式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# Filename: parasite_Chinese.py | |
#-*- coding:utf-8 -*- | |
import numpy.random as random | |
import cPickle as p | |
filedata = 'parasites.data' | |
def loaddict(filename): | |
f = file(filename) | |
dictionary = p.load(f) | |
return dictionary | |
def savedict(filename,dictionary): | |
f = file(filename, 'w') | |
p.dump(dictionary,f) | |
f.close() | |
return | |
def showall(dictionary): | |
print "Num. Name" | |
for item in dictionary: | |
print item, | |
print dictionary[item][0],'--',dictionary[item][1] | |
# main program | |
leave = False | |
parasite = loaddict(filedata) | |
while leave == False: | |
command = raw_input('>>>') | |
if command == 'add': | |
while True: | |
key = raw_input("Parasite dict's new key? ") | |
if key == 'q': | |
break | |
value = raw_input("Parasite dict's new value? ") | |
parasite[len(parasite) + 1] = (key, value) | |
print 'Added: ', parasite[len(parasite)],' and key is ',len(parasite) | |
if command == 'del': | |
showall(parasite) | |
which = input("Please enter the item's key number") | |
del parasite[which] | |
if command == 'show': | |
showall(parasite) | |
if command == 'test': | |
while True: | |
num = random.randint(1,len(parasite)+1) | |
question = parasite[num] | |
print "Question: ", question[0] | |
ans = raw_input('Ans? ') | |
if ans == 'q': | |
break | |
if ans == question[1]: | |
print 'CORRECT' | |
else: | |
print 'WRONG! The correct answer is ', question[1] | |
print '' | |
if command == 'q': | |
savedict(filedata, parasite) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment