TDT4109/Exercise 3/11 - Doble lokker/11d.py

63 lines
1.7 KiB
Python
Raw Normal View History

2020-09-14 18:40:56 +02:00
from random import randint
class multiplicationGame:
def __init__(self, min, max, tries):
self.min = min
self.max = max
self.tries = tries
self.updateProblem()
def generateNewMultiplicationProblem(self) -> (int, int):
number = lambda: randint(self.min, self.max)
self.currentProblem = (number(), number())
def updateProblem(self):
self.generateNewMultiplicationProblem()
self.currentTries = self.tries
def userWantsNewQuestion(self) -> bool:
while True:
answer = input(
'Er det ønskelig med flere spørsmål? Skriv 1 for ja og 0 for nei: ')
if answer in ['1', '0']:
return bool(int(answer))
else:
print('Skriv 1 for ja og 0 for nei')
def checkIfUserWantsNewQuestion(self):
if not self.userWantsNewQuestion():
exit(0)
def wrongAnswer(self):
self.currentTries -= 1
if self.currentTries == 0:
print(
'Dessverre klarte du ikke dette regnestykket, men vent så får du et nytt et:)'
)
self.checkIfUserWantsNewQuestion()
self.updateProblem()
else:
print(f'Dessverre ikke riktig. Du har {self.currentTries} forsøk igjen.')
def correctAnswer(self):
print('Gratulerer, det er helt riktig!')
self.checkIfUserWantsNewQuestion()
self.updateProblem()
def update(self):
answer = int(input(f'Hva blir {self.currentProblem[0]} * {self.currentProblem[1]}? '))
if answer == self.currentProblem[0] * self.currentProblem[1]:
self.correctAnswer()
else:
self.wrongAnswer()
def loop(self):
while True:
self.update()
if __name__ == "__main__":
game = multiplicationGame(min=0, max=10, tries=3)
game.loop()