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 9160517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:45:29+00:00 2026-06-17T13:45:29+00:00

This snippet of code was supposed to generate a random number which represents a

  • 0

This snippet of code was supposed to generate a random number which represents a question. The number generator generates numbers from 1 through 10. But if the number is not in the list of numbers "numlist" it is supposed to generate another number. This is supposed to make it so that the program won’t ask the same question twice. using numlist.remove() did not work for this purpose. What will work? Or, what is a better method.

Also, I want to know how to make it so that there is less repetition in the code (loop?).

def roll():
    var = random.randint(1,10)
    if var not in numlist:
        roll()

    elif var == 1:
        numlist.remove(1)
        q1()
    elif var == 2:
        numlist.remove(2)
        q2()
    elif var == 3:
        numlist.remove(3)
        q3()
    elif var == 4:
        numlist.remove(4)
        q4()
    elif var == 5:
        numlist.remove(5)
        q5()
    elif var ==6:
        numlist.remove(6)
        q6()
    elif var == 7:
        numlist.remove(7)
        q7()
    elif var == 8:
        numlist.remove(8)
        q8()
    elif var == 9:
        numlist.remove(9)
        q9()
    elif var == 10:
        numlist.remove(10)
        q10()
  • 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-06-17T13:45:30+00:00Added an answer on June 17, 2026 at 1:45 pm

    Keeping one function per question is not a good strategy. What if you want to change slightly how questions, hints and answers are given? You’re going to change dozens or even hundreds of functions?

    A much better approach is an object-oriented one- for example, where each question is an object of the Question class. For example:

    class Question:
        def __init__(self, question, hints, answer):
            self.question = question
            self.hints = hints
            self.answer = answer
    
        def ask_question(self):
            print "Here is your question:"
            print self.question
    
        def give_hint(self):
            if len(self.hints) == 0:
                print "That's all the hints I have!"
            else:
                print self.hints.pop(0)
    
         def guess(self, guess):
            if guess == self.answer:
                print "You guessed correctly!"
            else:
                print "No, try again!"
    

    Any behavior that you originally encapsulated in the question function (limiting the number of guesses, limited amount of time, displaying in a certain format, whatever) would all be handled by methods of the Question class. In the meantime, all the information specific to one question would be held in the data members (in this case question, hints and answers, although there could be other variables) that are specific to that question.

    You would create a question like this:

    q1 = Question("How many roads must a man walk down?", ["Think Douglas Adams.", "It's more than 40 and less than 50"], "42")
    

    Or better yet, create them from a tab delimited file, where the file is something like:

    How many roads must a man walk down?    Think Douglas Adams./It's more than 40 and less than 50    42
    

    And they are created like:

    questions = []
    with open("questions.txt") as inf:
        for l in inf:
            question, hints, answer = l[:-1].split("\t")
            questions.append(Question(question, hints.split("/"), answer))
    

    Then your main function would call methods of the Question, which encapsulate its question-asking behavior. This would keep you from ever having to repeat code (all the code exists only in one place: the methods of the Question object) and would keep all your questions in a flexible format.

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

Sidebar

Related Questions

Take this snippet of code which is supposed to replace a href tag with
This is a small snippet of code taken from some of the examples that
This code snippet is supposed to save part of a video whose range is
MonoTouch advertises support for AsParallel on its website with this code snippet: from item
The following code snippet is supposed to remove duplicates from a SQLite table: public
I have the following code snippet written in C which is supposed to retrieve
I have this code snippet that is supposed to test whether the user enters
This code snippet hand copied from a book I am reading: /* scmp: string
I would like someone to shed some light this code snippet, which confuses me.
This snippet of code always parses the date into the current timezone, and not

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.