Skip to content

Instantly share code, notes, and snippets.

@gabrielbissey
Last active March 5, 2018 23:06
Show Gist options
  • Save gabrielbissey/6a9512f7392111b10a5b6d23654d5b99 to your computer and use it in GitHub Desktop.
Save gabrielbissey/6a9512f7392111b10a5b6d23654d5b99 to your computer and use it in GitHub Desktop.
import string
easy_string = ("\nA neutron star is the result of a __1__. __1__e are the cataclysmic deaths of __2__ greater than the size of our own.\n" \
"Another result of __1__ are __3__. __3__ come from __2__ of even greater mass than those that create neutron stars.\n" \
"Neutron stars can be pushed to __1__ if too much __4__ falls into them.")
easy_answers = ['supernova', 'stars', 'black holes', 'mass']
easy_blanks = ['__1__','__2__','__3__','__4__']
medium_string = ("\nCaesar's De Bello Gallico\n" \
"\nGallia __1__ omnis divisa in partes tres, quarum unam incolunt __2__, aliam __3__, tertiam qui ipsorum lingua __4__, nostra __5__ appellantur.")
medium_answers = ['est', 'Belgae', 'Aquitani', 'Celtae', 'Galli']
medium_blanks = ['__1__','__2__','__3__','__4__','__5__']
hard_string = ("\nTrigonometry is the study of __1__. __1__ are more interesting than they might seem at first. With them, you can measure __2__. \n" \
"Using Trigonometry functions, such as Sine, __3__, and __4__, you can discover myriads of information, and compare them to other \n" \
"__1__ to do some pretty cool stuff.")
hard_answers = ['right triangles', 'distance', 'Cosine', 'Tangent']
hard_blanks = ['__1__','__2__','__3__','__4__']
count = 1
def import_params(selection):
#import_params takes as input the difficulty from run_quiz and it returns the corresponding string,
#blanks list, and answers.
if selection == 'easy':
return easy_string, easy_blanks, easy_answers, count
elif selection == 'medium':
return medium_string, medium_blanks, medium_answers, count
elif selection == 'hard':
return hard_string, hard_blanks, hard_answers, count
else:
return -1, -1, -1, -1
def num_check(error_quantity):
#num_convert takes as input error_quantity from run_quiz and returns its integer form.
#If error_quantity is not from 0 to 10 then it returns -1.
try:
int(error_quantity)
if int(error_quantity) >= 0:
return int(error_quantity)
else:
return -1
except ValueError:
return -1
def is_answer(user_input, string_answers, blank):
#is_answer takes as input user_input, string_answers, and blank sent from run_quiz.
#It returns True or False depending on whether the answer is correct.
blank = blank.split('__')
raw_number = blank[1]
set_index_position = -1
if user_input == string_answers[int(raw_number) + set_index_position]:
return True
else:
return False
def ask_for_answer(quiz_string, quiz_blanks, quiz_answers, count, error_quantity):
#ask_for_answer takes as input quiz_string, quiz_blanks, quiz_answers, count, and error_quantity
#sent from run_quiz. It returns the modified quiz_string if the answer is correct. If not then it
#returns an error or -1 depending on the state of error_quantity.
while count <= len(quiz_blanks):
blank = '__' + str(count) + '__'
if blank in quiz_string:
print(quiz_string)
user_input = raw_input('\nWhat shall the replacement be for ' + str(blank) + ' , Sire? \n:')
answer = is_answer(user_input, quiz_answers, blank)
if answer == True:
quiz_string = string.replace(quiz_string, blank, user_input)
count += 1
elif answer == False and error_quantity > 1:
print ('\nIncorrect. Try again.\n' + str(error_quantity) + ' attempts left.')
error_quantity -= 1
elif answer == False and error_quantity == 1:
print ('\nIncorrect. Try again.\n' + str(error_quantity) + ' attempt left.')
error_quantity -= 1
elif answer == False and error_quantity == 0:
return -1
def run_quiz():
#run_quiz takes as input the difficulty and amount of errors allowed. It is the main function that
#ties all the other functions together. If user gets through the quiz then it returns 'YOU WON'.If
#the user does not get through quiz then it returns 'GAME OVER'.
while True:
selection = raw_input('Select a difficulty:\neasy | medium | hard\n:')
quiz_string, quiz_blanks, quiz_answers, count = import_params(selection)
if quiz_string == -1:
print ('\nInvalid difficulty. Try again.\n')
else:
while True:
number_input = raw_input('\nHow many errors are allowed?\n:')
error_quantity = num_check(number_input)
if error_quantity == -1:
print ('\nInvalid number, try again.\nType number not word (ex."1" not "one").\nNumber must be greater than or equal to zero.')
else:
result = ask_for_answer(quiz_string, quiz_blanks, quiz_answers, count, error_quantity)
if result == -1:
return ('\n*************\n* GAME OVER *\n*************')
else:
return ('\n************\n* YOU WON! *\n************')
print run_quiz()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment