Last active
November 9, 2016 13:26
-
-
Save JellyWX/c2bdb7b3004216e3dce234a7ac4363be to your computer and use it in GitHub Desktop.
Small quiz game that loads a quiz from a file. Example of using string methods to cut and pick parts from strings and lines of files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qinit | |
q:What team does NiKo play for? | |
a:mousesports | |
q:How much is the AWP Sniper Rifle (raw numbers)? | |
a:4750 | |
q:What is the terrorist equivalent to the SCAR-20? | |
a:g3sg1 | |
q:What is the default counter terrorist equivalent to the AK-47? | |
a:m4a4 | |
q:How many bullets does the 5.7 have total? | |
a:120 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import listdir | |
from sys import exit | |
from random import choice | |
score = 0 | |
q = [] | |
a = [] | |
r = ['Nice', 'Well done', 'Good job', 'Great', '+1, dude', 'Nice going', 'Good going', 'Cool', 'Neat'] | |
def rline(y, n): ##A function to read individual lines out of files. | |
f_rline = open(y, 'r') | |
i = 1 | |
for line in f_rline: | |
if i == n: | |
return line.splitlines()[0] | |
f.close | |
break | |
i+=1 | |
def response(x): ##Provides a message dependant on score | |
if x == 100: | |
return 'Perfect!' | |
elif x > 60: | |
return 'Well done!' | |
elif x > 40: | |
return 'Nice!' | |
else: | |
return 'Try again maybe?' | |
def main(): | |
name = str(raw_input('Please enter your name > ')) | |
print('Hello ' + name + '! Reading available quiz files...') | |
found_file = False | |
for file in listdir('quizzes'): | |
if rline('quizzes/' + file, 1) == 'qinit': | |
if file.endswith('.txt'): | |
print('-- ' + file.split('.')[0] + '\n') | |
found_file = True | |
if found_file == False: | |
print('Fatal: no files available. Closing') | |
exit('Fatal: no files matching requirements in directory quizzes/. The file must start with qinit on the first line') | |
else: | |
print('Type the name of one of the files above to begin.\nYou will be asked a question and have to answer it.\nThe quiz is not case sensitive, however different spellings of the answer will not work.\n~~\nYour scores will be logged and can be viewed at the end of the quiz.') | |
file_open = False | |
while(file_open == False): | |
try: | |
try: | |
f_i = raw_input('Enter the file name below > ') | |
except: | |
print('error in inputted information') | |
f = open('quizzes/' + f_i + '.txt', 'r') | |
file_open = True | |
except: | |
print('Failed to open file.') | |
file_open = False | |
for line in f: | |
if line.split(':')[0] == 'q': | |
q.append(line.split(':')[1].splitlines()[0]) | |
elif line.split(':')[0] == 'a': | |
a.append(line.split(':')[1].splitlines()[0]) | |
questions = len(q) | |
print('The quiz you have selected has ' + str(questions) + ' readable questions. Enjoy!') | |
guesses = 0 | |
score = 0 | |
score_addon = 5 | |
j = 0 | |
correct = False | |
while j != questions: | |
guesses = 0 | |
score_addon = 5 | |
correct = False | |
while guesses < 3: | |
ans_u = str(raw_input(q[j] + ' > ')) | |
if ans_u.upper().replace(' ','') == a[j].upper().replace(' ',''): | |
score += score_addon | |
print('Correct! ' + choice(r)) | |
break | |
else: | |
score_addon -= 2 | |
guesses += 1 | |
print('Try again!') | |
if guesses > 2: | |
print('Incorrect! The correct answer was ' + a[j].title() + '.') | |
j += 1 | |
percent = (float(score)/float(questions*5))*100 | |
print('You got ' + str(percent) + '% questions correct. (' + str(score) + ' out of ' + str(questions*5) + '). ' + response(percent)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment