Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5939511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:50:19+00:00 2026-05-22T15:50:19+00:00

Creating function to print question, add choices to a list. def print_et_list (): answer_list

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:50:19+00:00Added an answer on May 22, 2026 at 3:50 pm

    To correct the infinite loop, avoid to re-open the file on each call to print_et_list()

    Try this (I renamed function into file_handle to be a little more explicit while reading the code)

    import sys
    
    def print_et_list (file_handle):
        answer_list = []
        #Question
        question = file_handle.readline()
        print question
        #Choices
        one = file_handle.readline()
        two = file_handle.readline()
        answer_list.append(one)
        answer_list.append (two)
        for item in answer_list:
            print item
        #Solution
        solution = None
        try:
            result = file_handle.readline()
            result.replace("\n","")
            solution = int(result)
        except:
            print "There's an error in the answer"
    
        ##for the blank line
        file_handle.readline()
        return question, one, two, solution
    
      ##file_handle 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"))
        print "you answered '%s'"%user_answer
        if user_answer == solution:
            print "You've got it right!"
            score += 1
        elif user_answer == 0:
            sys.exit()
        else:
            print "You've got it wrong."
        return score
    
    def main ():
            file_handle = open ("modStory.txt","r")
            question, one, two, solution = print_et_list(file_handle)
            scoresofar = hey_user(solution)
            print "\nYour score is now", scoresofar
            while question:
                question, one, two, solution = print_et_list(file_handle)
                if question:
                    scoresofar = hey_user(solution, scoresofar)
                    print "\nYour score is now", scoresofar
            file_handle.close()
    
    main ()
    
    
    raw_input ("Hit enter to exit.")
    

    This is not a perfect version, but it seems to work 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a function where I need to pass an object so that it
I've got a function creating some XmlDocument: public string CreateOutputXmlString(ICollection<Field> fields) { XmlWriterSettings settings
I'm having trouble creating a mouseover function with an NSTableView. The idea is that
I'm doing this system Stacked and I am creating the search function. And in
Currently I have the function CreateLog() for creating a a log4net Log with name
I am manually creating the equivalent lambda: var function = p => p.Child.Any(c =>
I'm working on creating a call back function for an ASP.NET cache item removal
I'm creating my own dictionary and I am having trouble implementing the TryGetValue function.
I'm wondering what the best method is for creating a forgot password function on
I have a strange question concerning subroutines: As I'm creating a minimal language and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.