Creating function to print question, add choices to a list.
def print_et_list ():
answer_list = []
function = open ("modStory.txt","r")
#Question
question = function.readline()
print question
#Choices
one = answer_list.append (function.readline())
two = answer_list.append (function.readline())
for item in answer_list:
print item
#Solution
try:
solution = int(function.readline())
except:
print "There's an error in the answer"
##for the blank line
function.readline()
return question, one, two, solution, function
##Function for prompting the user for an answer, comparing an answer, keeping score and printing score.
def hey_user (solution):
score = 0
user_answer = int(raw_input ("Enter what you think the answer is, user.\n"))
if user_answer == solution:
print "You've got it right!"
score = score + 1
elif user_answer == 0:
sys.exit()
else:
print "You've got it wrong."
return score
def main ():
question, one, two, solution, function = print_et_list()
scoresofar = hey_user (solution)
print "\nYour score is now", scoresofar
while question:
question, one, two, solution, function = print_et_list()
function.close()
main ()
raw_input ("Hit enter to exit.")
For some reason I am unable to get this thing to loop properly. The code above infinte loops itself.
This following is the text file in question which is just garbled song lyrics. The program will run the first fragment properly, and will infinte loop the first fragment once the user gives the answer.
Can you carry my drink I have everything else
1 - I can tie my tie all by myself
2 - I'm getting tired, I'm forgetting why
2
is diving diving diving diving off the balcony
1 - Tired and wired we ruin too easy
2 - sleep in our clothes and wait for winter to leave
1
While it sings to itself or whatever it does
1 - when it sings to itself of its long lost loves
2 - I'm getting tired, I'm forgetting why
2
To correct the infinite loop, avoid to re-open the file on each call to
print_et_list()Try this (I renamed
functionintofile_handleto be a little more explicit while reading the code)This is not a perfect version, but it seems to work 😉