Advertisement
Guest User

LPTHW Q and A game/ ex46

a guest
Aug 1st, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. from ex46module import quest
  2. from sys import exit
  3. import random
  4.  
  5. #random.sample iterates over the keys in dict quest and returns 1 key
  6.  
  7. keys = random.sample(quest.items(),len(quest)) #passing .items method returns a list w/ key:value pairs
  8. # keys is now a list of Tuples that can be iterated over
  9.  
  10.  
  11. #print random.sample(keys[0],1) # this is taking a random selection either key or value in
  12. #in random tuple from keys
  13.  
  14.  
  15.  
  16. #k1 = keys[0] # k1 is set to the Tuple in the list generated
  17.  
  18. #str(keys[0]) #since tuple is at index position 0, returns the tuple (as a string)
  19.  
  20. #print k1[0] # returns first value in Tuple, index 0, the key
  21. #k1[1] # returns second value in Tuple, index 1, the value
  22.    
  23. def q_and_a(quest):
  24.     '''
  25.     This takes as input a dict of questions /answers,
  26.     global vars, keys, k1, are loopsd and tested to
  27.     remove a question that has been used and if ans is correct
  28.     or wrong.
  29.     '''
  30.     k1 = keys[0]
  31.     #str(keys[0])
  32.     print k1[0]
  33.     k1[1]
  34.     ans = raw_input(">  ")
  35.    
  36.     qlist = [] # empty list to hold questions as selected
  37.     qcount = len(qlist) # starts at Zero
  38.     correct_ans_count = 0
  39.    
  40. # Ans at this point is stuck and is not updating, that's why all answers are correct
  41. # How do I update the value for ans?
  42.    
  43.     while True: # qcount <= 5:
  44.         if ans == k1[1]: # or new_ans: # if answer is correct /was while/
  45.             print ans, k1[1]
  46.             qcount += 1 # updates # of questions asked
  47.             correct_ans_count += 1 # updates # of correct answers
  48.             print 'That\'s Correct!'
  49.             print 'You have %d questions to go' %(3 - qcount,)
  50.             print 'Correct Answers: ',correct_ans_count
  51.             if correct_ans_count == 3: #ans == k1[1] and  #Test if ready to advance to new room
  52.                 print 'Congratulations, on to the next room'
  53.                 return
  54.             #return next_room
  55.         else:
  56.             if ans != k1[1]:
  57.                 print 'Wrong Answer'
  58.                 qcount += 1
  59.                 print qcount
  60.                 if qcount == 5:
  61.                     exit(1)
  62.                     #return banishment
  63.         qlist.append(keys[0]) # append the question to do not use list (qlist)
  64.         print qlist
  65.         for i in keys:  #next 3 line compare used question to keys and if matched
  66.             if i in qlist: # ...removes tuple from keys
  67.                 keys.remove(i)
  68.                 new_question = keys[0][0] # selects new question
  69.                 print new_question
  70.                 global ans
  71.                 ans = keys[0][1]
  72.                 print ans
  73.                 new_ans = raw_input("> ")
  74.                 ans = new_ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement