class programming Treasurechest
unknown
python
a year ago
1.8 kB
4
Indexable
class TreasureChest: #_question : STRING #_answer : INTEGER #_points : INTEGER def __init__(self,Thequestion, Theanswer , Thepoints): self.__question = Thequestion self.__answer = Theanswer self.__points = Thepoints def get_questions(self): return self.__question def getPoints(self, attempts): if attempts == 1: return int(self.__points) elif attempts == 2: return int(self.__points)//2 elif attempts == 3 or attempts == 4: return int(self.__points)//4 else: return 0 def checkAnswer(self, useranswer): if int(self.__answer) == useranswer: return True else: return False #arrayTreasure (5) as TreasureChest def readData(): #arrayTreasure = [] try: file = open("TreasureChestData.txt","r") data = (file.readline().strip()) while data != "": question = data answer = (file.readline().strip()) points = (file.readline().strip()) arrayTreasure.append(TreasureChest(question, answer, points)) data = (file.readline().strip()) file.close() except IOError: print("cannot find file") arrayTreasure = [] readData() choice = int(input("enter a question number between 1 and 5 :")) if choice > 0 and choice < 6: result = False attempts = 0 while result == False: answer = int(input(arrayTreasure[choice-1].get_questions())) result = arrayTreasure[choice-1].checkAnswer(answer) print(result) attempts += 1 print(int(arrayTreasure[choice-1].getPoints(attempts)))
Editor is loading...
Leave a Comment