Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. import math
  6. from os import path
  7.  
  8. SAVE_FILE = '/usr/bin/.grade1'
  9.  
  10. QUESTIONS = [
  11. 'Question 1) What is the answer to level 1?',
  12. 'Question 2) What is the answer to level 2?',
  13. ]
  14.  
  15. ANSWERS = ['', '000ff2bf441600488eb474a24fa1dce3012449ea']
  16.  
  17. SCORE = 0
  18.  
  19. CONGRATULATIONS = '''CONGRATULATIONS!!!'''
  20.  
  21. AWESOME = '''Awesome :D'''
  22.  
  23. FAIL = '''Fail :('''
  24.  
  25. def get_input():
  26. ans = raw_input('> ')
  27. return ans.strip()
  28.  
  29. def load_level():
  30. global SCORE
  31. try:
  32. f = open(SAVE_FILE, 'rt')
  33. level = int(f.read().strip())
  34. f.close()
  35. SCORE = level * 10
  36. print 'Your current score is %d' % SCORE
  37. except:
  38. level = 0
  39. return level
  40.  
  41. def write_level(l):
  42. f = open(SAVE_FILE, 'wt')
  43. f.write(str(l))
  44. f.close()
  45.  
  46. def check_ans(l, ans):
  47. if(l == 0):
  48. try:
  49. nums = ans.split()
  50. a = float(nums[0])
  51. b = float(math.log(float(nums[1]), 10))
  52. if(a == b):
  53. return True
  54. except:
  55. return False
  56.  
  57. import hashlib
  58. return hashlib.sha1(ans).hexdigest() == ANSWERS[l]
  59.  
  60. def ask_question(l):
  61. print QUESTIONS[l]
  62. if(l != 2):
  63. ans = get_input()
  64. else:
  65. ans = ''
  66. if check_ans(l, ans):
  67. correct(l)
  68. else:
  69. lose(l)
  70.  
  71. def correct(l):
  72. global SCORE
  73. write_level(l + 1)
  74. SCORE = SCORE + 10
  75. print AWESOME
  76. print '+10 marks'
  77. if l == len(QUESTIONS) - 1:
  78. win()
  79. else:
  80. print 'Your current score is %d' % SCORE
  81. print 'You have %d questions to go!' % (len(QUESTIONS) - l - 1)
  82. print ''
  83.  
  84.  
  85. def win():
  86. print CONGRATULATIONS
  87. print 'Your final score is %d' % SCORE
  88. raw_input('Press any key to continue...')
  89. exit()
  90.  
  91. def lose(l):
  92. print FAIL
  93. print 'Your current score is %d' % SCORE
  94. print 'You have %d questions to go!' % (len(QUESTIONS) - l)
  95. raw_input('Press any key to continue...')
  96. exit()
  97.  
  98. if __name__=="__main__":
  99. if(len(sys.argv) == 2):
  100. if(sys.argv[1] == 'r'):
  101. write_level(0)
  102. l = load_level()
  103. if l == len(QUESTIONS):
  104. win()
  105. else:
  106. for i in range(l, len(QUESTIONS)):
  107. ask_question(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement