Advertisement
Domy131097

Untitled

Dec 3rd, 2018
131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None
  1. #-------------------------------------------------------------------------- IMPORTS ---------------------------------------------------------------------------
  2. import time, random, pickle
  3. #------------------------------------------------------------------------ DEFINITIONS -------------------------------------------------------------------------
  4. selection = -1
  5. money = 0
  6. numOfCorrectAnswer = 0
  7. #--------------------------------------------------------------------- readScore FUNCTION ---------------------------------------------------------------------
  8. def readScore():
  9. scores = open('scores.pkl', 'rb')
  10. highscores = pickle.load(scores)
  11. scores.close()
  12. return highscores
  13. #--------------------------------------------------------------------- saveScore FUNCTION ---------------------------------------------------------------------
  14. def saveScore(highscores):
  15. scores = open('scores.pkl', 'wb')
  16. pickle.dump(highscores[:10], scores)
  17. scores.close()
  18. #---------------------------------------------------------------------- setScore FUNCTION ---------------------------------------------------------------------
  19. def setScore(playerName):
  20. global money
  21. highscores = []
  22. highscores = readScore()
  23. highscores.append((money, playerName))
  24. highscores = sorted(highscores, reverse=True)
  25. saveScore(highscores)
  26. readScore()
  27. #-------------------------------------------------------------------- printScore FUNCTION ---------------------------------------------------------------------
  28. def printScore():
  29. highscores = []
  30. name = []
  31. score = []
  32. highscores = readScore()
  33. for i in range(10):
  34. name.append(highscores[i][1])
  35. score.append(highscores[i][0])
  36. print "--------------- TOP 10 PLAYERS ---------------"
  37. for i in range(10):
  38. print str(i + 1) + ". " + name[i] + " " + str(score[i]) + "kn"
  39. print "----------------------------------------------"
  40. time.sleep(3)
  41. return
  42. #---------------------------------------------------------------- checkIsNumberInArray FUNCTION ---------------------------------------------------------------
  43. def checkIsNumberInArray(i, num, questionNumbers):
  44. for x in range(i):
  45. if questionNumbers[x] == num:
  46. return 1
  47. return 0
  48. #--------------------------------------------------------------- randomSelectQuestions FUNCTION ---------------------------------------------------------------
  49. def randomSelectQuestions(count):
  50. questionNumbers = []
  51. for i in range(15):
  52. while True:
  53. num = random.randint(0, count - 1)
  54. if checkIsNumberInArray(i, num, questionNumbers) == 0:
  55. questionNumbers.append(num)
  56. break
  57. return questionNumbers
  58. #-------------------------------------------------------------------- askQuestion FUNCTION --------------------------------------------------------------------
  59. def askQuestion(i, quesNum, questions, price):
  60. global money, numOfCorrectAnswer
  61. myanswer = "Ne znam"
  62. question = questions[quesNum][0]
  63. correctAnswer = questions[quesNum][1]
  64. answer1 = questions[quesNum][2]
  65. answer2 = questions[quesNum][3]
  66. answer3 = questions[quesNum][4]
  67. answer4 = questions[quesNum][5]
  68.  
  69. print "Pitanje %d (za %dkn): " % ((i + 1), price) + "\n" + question
  70. time.sleep(1)
  71. print "a) " + answer1
  72. time.sleep(1)
  73. print "b) " + answer2
  74. time.sleep(1)
  75. print "c) " + answer3
  76. time.sleep(1)
  77. print "d) " + answer4
  78. selection = raw_input("Unesite tocan odgovor (a/b/c/d): ")
  79. if selection == 'a':
  80. myanswer = answer1
  81. elif selection == 'b':
  82. myanswer = answer2
  83. elif selection == 'c':
  84. myanswer = answer3
  85. elif selection == 'd':
  86. myanswer = answer4
  87. if myanswer == correctAnswer:
  88. print "----------------------------------------------\n" + "Vas odgovor je tocan, trenutno imate %dkn" % price + "\n----------------------------------------------\n"
  89. money = price
  90. numOfCorrectAnswer += 1
  91. return 1
  92. else:
  93. print "----------------------------------------------\n" + "Vas odgovor nije tocan!\n=> Tocan odgovor: " + correctAnswer + "\n----------------------------------------------\n"
  94. return 0
  95. #------------------------------------------------------------------ selectQuestion FUNCTION -------------------------------------------------------------------
  96. def selectQuestion(playerName, questions, count):
  97. global money, numOfCorrectAnswer
  98. money = 0
  99. numOfCorrectAnswer = 0
  100. questionNumbers = randomSelectQuestions(count)
  101. for i in range(len(questionNumbers)):
  102. if i == 0:
  103. price = 100
  104. else:
  105. price += price
  106. if askQuestion(i, questionNumbers[i], questions, price) == 0:
  107. break
  108. print "----------------------------------------------\n" + "Odgovorili ste tocno na %d od 15 pitanja.\nOsvojili ste %dkn." % (numOfCorrectAnswer, money) + "\n----------------------------------------------\n"
  109. setScore(playerName)
  110. time.sleep(3)
  111. #--------------------------------------------------------------------- readFile FUNCTION ----------------------------------------------------------------------
  112. def readFile(playerName):
  113. count = 0
  114. questions = []
  115.  
  116. openedFile = open("questions.txt", "r")
  117. if openedFile.mode == "r":
  118. loadRow = openedFile.readlines()
  119. for oneLine in loadRow:
  120. sentence = oneLine.split("#")
  121. questions.append(sentence)
  122. count += 1
  123. selectQuestion(playerName, questions, count)
  124. #--------------------------------------------------------------------- addAnswer FUNCTION ---------------------------------------------------------------------
  125. def addAnswer():
  126. f = open("questions.txt","a+")
  127. question = raw_input("Unesite pitanje: ")
  128. f.write(question + "#")
  129. correctAnswer = raw_input("Unesite odgovor na uneseno pitanje: ")
  130. f.write(correctAnswer + "#")
  131. for i in range(4):
  132. answer = raw_input("Unesite odgovor %d: " % (i+1))
  133. if i < 3:
  134. f.write(answer + "#")
  135. else:
  136. f.write(answer + "\n")
  137. f.close
Advertisement
RAW Paste Data Copied
Advertisement