iamaamir

Quiz_Game.py

Apr 5th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | None | 0 0
  1. # Defining quizes by game level from easy to difficult.
  2.  
  3. # Easy:
  4. quiz1 = '''Python is a high-level, interpreted, interactive and object-oriented scripting ___1___. Python is ___2___ to be highly readable. It uses English ___3___ frequently where other languages use ___4___.'''
  5.  
  6. # Medium:
  7. quiz2 = '''A group of individual ___1___, which make a single code block are called ___2___ in Python.
  8. Compound or complex statements, such as if, while, def, and class require a ___3___ line and a ___4___.'''
  9.  
  10. # Difficult:
  11. quiz3 = ''' A ___1___ is a sequence of ___2___ Python objects. They are ___3___, just like lists.
  12. They cannot be changed unlike lists, and they use parentheses. Lists use square ___4___. '''
  13.  
  14.  
  15. quiz_answers = {'easy': ["language", "designed", "keywords", "punctuation"],
  16.                 'medium': ["statements", "suites", "header", "suite"],
  17.                 "difficult": ["tuple", "inmutable", "sequences", "brackets"]}
  18.  
  19.  
  20. print "\nHELLO!!!Welcome to my Quiz!\n\nInstructions:\nThis game consist of three difficulty levels (easy, medium and difficult). Each level requires you to fill-in four blanks. To start the game, you will have to choose the level you would like to complete first. As you complete each level, you will be automatically moved to the next level in the list of hierarchy until you complete all of them satisfactorily.\nGood Luck\n!"
  21.  
  22.  
  23. def start():
  24.     global level
  25.     level = raw_input(
  26.         "Please select an easy, medium or difficult level. Enter your choice here: ")
  27.     validate_level()
  28.  
  29.  
  30. def validate_level():
  31.     level_list = ["easy", "medium", "difficult"]
  32.  
  33.     if level not in level_list:
  34.         print "\nWrong spelling. Sorry."
  35.         start()
  36.     else:
  37.         print "\nCongratulations! You chose the {} level. You are ready to play!\n".format(level)
  38.         validate_answer()
  39.  
  40.  
  41. def filled_quizzes():
  42.  
  43.     quiz1_a = quiz1.replace("___1___", "language")
  44.     quiz1_b = quiz1_a.replace("___2___", "designed")
  45.     quiz1_c = quiz1_b.replace("___3___", "keyword")
  46.     quiz1_d = quiz1_c.replace("___4___", "punctuation")
  47.  
  48.     return (quiz1_a, quiz1_b, quiz1_c, quiz1_d)
  49.  
  50.  
  51. def quiz_answer_list():
  52.  
  53.     if level == 'easy':
  54.         print "\nFill in the blanks.\n\n" + quiz1 + "\n"
  55.     if level == 'medium':
  56.         print "\nFill in the blanks.\n\n" + quiz2 + "\n"
  57.     if level == 'difficult':
  58.         print "\nFill in the blanks.\n\n" + quiz3 + "\n"
  59.  
  60.     return quiz_answers.get(level, None)
  61.  
  62.  
  63. def validate_answer():
  64.     answers_list = quiz_answer_list()
  65.     filled_quiz = filled_quizzes()
  66.     position_in_list = 0
  67.     final_position_list = 3
  68.     while True:
  69.         user_input = raw_input("Enter Answer here: ")
  70.         while user_input != answers_list[position_in_list]:
  71.             if position_in_list < final_position_list:
  72.                 print "\nWrong spelling. Please try again."
  73.                 break
  74.             else:
  75.                 return "\nYou exceeded the number of trials. \nGame over.Thank you for playing!\n"
  76.  
  77.         while user_input == answers_list[position_in_list]:
  78.             if position_in_list < final_position_list:
  79.                 print "\nCongratulations! Your answer is correct.\n\n" + filled_quiz[position_in_list] + "\n\nPlease continue. "
  80.                 position_in_list = position_in_list + 1
  81.             else:
  82.                 print "\nCongratulations! Your answer is correct.\n\nYou completed the level!!!PLease continue.\n\n________________________________\n\n"
  83.                 return "Todo bien"
  84.  
  85.  
  86. def next_level():
  87.  
  88.     level_list = ["easy", "medium", "difficult"]
  89.     global level
  90.     if level == level_list[0]:
  91.         level = level_list[1]
  92.         return validate_answer()
  93.     elif level == level_list[1]:
  94.         level = level_list[2]
  95.         return validate_answer()
  96.     else:
  97.         if level == level_list[2]:
  98.             print "Congratulations!!! You completed the highest level.\nThe game is over.\n\n"
  99.             return play_again()
  100.  
  101.  
  102. def play_again():
  103.     user_input = raw_input("To play again enter code 333 here: ")
  104.     if user_input == "333":
  105.         return start()
  106.     else:
  107.         print "A wrong code was entered. Try again."
  108.         play_again()
  109.  
  110.  
  111. if __name__ == '__main__':
  112.     start()
Add Comment
Please, Sign In to add comment