Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #SkillsUSA Computer Programming Problem Number One - Math Challenge
  2.  
  3. import random
  4. import time
  5.  
  6. class Math_game(object):
  7.  
  8. def __init__(self):
  9.  
  10. print("Welcome to the Official Math Game of SkillsUSA!nYou will have at least five relatively simple questions,nand up to ten difficult ones.nGood luck!")
  11. time.sleep(5)
  12. x = 3
  13. while x != 0:
  14. print("Starting in ",x,"...")
  15. time.sleep(1)
  16. x -= 1
  17.  
  18. #Above we can see a cheesy intro with the time countdown.
  19.  
  20.  
  21. self.tried = 0
  22. self.correct = 0
  23. self.easyQuestionsAsked = 0
  24. self.numCorrect = -1
  25.  
  26.  
  27. #The __init__ method is simply the initiating object. When an object of the class is made, this method/function is automatically called.
  28. #By automatically declaring all of these objects, we can start keeping track of the number of questions attempted, correct, etc.
  29.  
  30. def prob(self, maximum, operators):
  31.  
  32. self.maximum = maximum
  33. self.operators = operators
  34. self.num_a, self.num_b = [random.randint(1,self.maximum),random.randint(1,self.maximum)]
  35.  
  36. #In the above line, we are able to declare two variable values in one, efficient line.
  37.  
  38. operators = ['+','-','*']
  39.  
  40. self.operator = operators[random.randint(0,int(self.operators))]
  41.  
  42. #The operators parameter of the "prob" method is used to access the list of operators, If only two operators are available to use, a random number
  43. #chooses between + or -. Otherwise, it will choose between + or - or *.
  44.  
  45. if self.maximum < 50:
  46.  
  47. if self.num_a <= self.num_b:
  48. self.num_a = random.randint(1,self.maximum)
  49.  
  50. #If it is an easy problem and we are not allowed to use negatives, and the first number is smaller than the second, just default to 25.
  51.  
  52. if self.operator== '+':
  53. self.answer = self.num_a + self.num_b
  54. elif self.operator == '-':
  55. self.answer = self.num_a - self.num_b
  56. else:
  57. self.num_a, self.num_b = random.randint(-12,12), random.randint(-12,12)
  58. # If we are using the multiplication operator, set the numbers to max out at 12, or be as low as -12.
  59. self.answer = self.num_a * self.num_b
  60.  
  61.  
  62.  
  63. #Above, we use the parameter, userself.answer, and see if the self.answer is correct. Below, we the returned value is whether the user correctly self.answered the question.
  64.  
  65. return self.num_a, self.operator, self.num_b
  66.  
  67.  
  68. def isCorrect(self, userInput):
  69.  
  70. self.userInput = userInput
  71. #Simply returning whether the user answer was correct or not.
  72. return self.userInput == self.answer
  73.  
  74.  
  75.  
  76. game = Math_game()
  77. #Make an object of the Math_game class.
  78. while game.tried <= 15:
  79. if game.easyQuestionsAsked <= 5:
  80. game.prob(25,1)
  81. game.easyQuestionsAsked += 1
  82. #While we have not asked 15 questions yet, and while there have been less than 5 easy questions asked, ask another easy question.
  83. else:
  84. game.prob(50,2)
  85. userAnswer = int(input("Problem: {0} {1} {2} : ".format(game.num_a, game.operator, game.num_b)))
  86. #If we do not HAVE to ask an easy question, feel free to ask a hard one.
  87.  
  88. if game.isCorrect(userAnswer):
  89. game.numCorrect += 1
  90. print("Right!")
  91. else:
  92. print("Wrong!")
  93. game.tried += 1
  94. #Accumalting questions asked.
  95.  
  96. if game.numCorrect > 11:
  97. print("Great job! ({0}/15)".format(game.numCorrect))
  98. else:
  99. print("Try again for a better score! ({0}/15)".format(game.numCorrect))
  100. print("This window will automatically close in ten seconds.")
  101.  
  102. time.sleep(10)
  103. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement