Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import sys
- import math
- from os import path
- SAVE_FILE = '/usr/bin/.grade1'
- QUESTIONS = [
- 'Question 1) What is the answer to level 1?',
- 'Question 2) What is the answer to level 2?',
- ]
- ANSWERS = ['', '000ff2bf441600488eb474a24fa1dce3012449ea']
- SCORE = 0
- CONGRATULATIONS = '''CONGRATULATIONS!!!'''
- AWESOME = '''Awesome :D'''
- FAIL = '''Fail :('''
- def get_input():
- ans = raw_input('> ')
- return ans.strip()
- def load_level():
- global SCORE
- try:
- f = open(SAVE_FILE, 'rt')
- level = int(f.read().strip())
- f.close()
- SCORE = level * 10
- print 'Your current score is %d' % SCORE
- except:
- level = 0
- return level
- def write_level(l):
- f = open(SAVE_FILE, 'wt')
- f.write(str(l))
- f.close()
- def check_ans(l, ans):
- if(l == 0):
- try:
- nums = ans.split()
- a = float(nums[0])
- b = float(math.log(float(nums[1]), 10))
- if(a == b):
- return True
- except:
- return False
- import hashlib
- return hashlib.sha1(ans).hexdigest() == ANSWERS[l]
- def ask_question(l):
- print QUESTIONS[l]
- if(l != 2):
- ans = get_input()
- else:
- ans = ''
- if check_ans(l, ans):
- correct(l)
- else:
- lose(l)
- def correct(l):
- global SCORE
- write_level(l + 1)
- SCORE = SCORE + 10
- print AWESOME
- print '+10 marks'
- if l == len(QUESTIONS) - 1:
- win()
- else:
- print 'Your current score is %d' % SCORE
- print 'You have %d questions to go!' % (len(QUESTIONS) - l - 1)
- print ''
- def win():
- print CONGRATULATIONS
- print 'Your final score is %d' % SCORE
- raw_input('Press any key to continue...')
- exit()
- def lose(l):
- print FAIL
- print 'Your current score is %d' % SCORE
- print 'You have %d questions to go!' % (len(QUESTIONS) - l)
- raw_input('Press any key to continue...')
- exit()
- if __name__=="__main__":
- if(len(sys.argv) == 2):
- if(sys.argv[1] == 'r'):
- write_level(0)
- l = load_level()
- if l == len(QUESTIONS):
- win()
- else:
- for i in range(l, len(QUESTIONS)):
- ask_question(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement