furas

Python - Quiz (reddit.com/u/learnpython)

Aug 26th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. #
  2. # https://www.reddit.com/r/learnpython/comments/4zosbr/an_example_for_beginners_to_use/
  3. # http://pastebin.com/6gZEgVi2
  4. #
  5.  
  6. import random
  7. import time
  8.  
  9. # questions and answers list :
  10.  
  11. data = [
  12.     ("Does int(x) make x into an integer?", "yes"),
  13.     ("Is Polkagrisen087 a good streamer?", "yes"),
  14.     ("Is Justin Bieber a singer?", "yes"),
  15.     ("Is Alexander Gay?", "no"),  # Are you sure ?
  16. ]
  17.  
  18.  
  19. class QuestionAsker():
  20.  
  21.     def __init__(self):
  22.             self.answers = 0
  23.  
  24.             self.display_help()
  25.             self.run()
  26.  
  27.     def display_help(self):
  28.  
  29.             print
  30.             print "=== The Quiz ==="
  31.             print
  32.             print "If invalid, scrip crashes"
  33.             print
  34.             print "Acceptable strings:"
  35.             print " yes"
  36.             print " no"
  37.             print " c - show result"
  38.             print " h - display this help"
  39.             print
  40.  
  41.     def select_question(self):
  42.         self.question, self.answer = random.choice(data)
  43.  
  44.     def run(self):
  45.  
  46.         self.select_question()
  47.  
  48.         while True:
  49.             user_in = raw_input(self.question).strip().lower()
  50.  
  51.             if user_in == "c":
  52.                 print "Answers:", self.answers
  53.             if user_in == "h":
  54.                 self.display_help()
  55.             elif user_in == self.answer:
  56.                 print "that is correct!"
  57.                 self.answers += 1
  58.                 self.select_question()
  59.             else:
  60.                 print "Wrong answer, you are not allowed to play my quiz"
  61.                 time.sleep(1)
  62.                 #raise TypeError("Invalid input") # Crashing the script.
  63.                 return # exit funtion (and end script)
  64.  
  65. if __name__ == "__main__":
  66.     QuestionAsker()
Add Comment
Please, Sign In to add comment