Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I'm new to python and don't realize a lot however figure I could make tests quite well and need them to be somewhat convoluted. 

How would I make it with the goal that the program will initially have the client pick the sort of test and afterward the inquiries given to the client will be about that subject. Would it be a good idea for me to make a function with the arrangement of inquiry for every theme? At that point when the client inputs the theme they need, the code should look something like

print("Do you want to answer questions on animals or capital cities or math?"

      " Type animal, city or math")

topic = input()

if topic == 'animal':

    def AnimalQuestions(): #this will be written before this code

Is this the right methodology or is there another, more proficient approach to do it?

closed

4 Answers

0 votes
by (19k points)
 
Best answer

To create a program where users can select a test topic and answer questions based on their choice, follow these steps:

  1. Define separate functions for each topic, such as animal_questions(), city_questions(), and math_questions(). Each function should contain the relevant set of questions and handle user responses.

  2. Prompt the user to select a topic and store their input.

  3. Call the corresponding function based on the user's choice to initiate the test.

Here's a condensed version of the code:
def animal_questions():
    # Handle questions about animals
def city_questions():
    # Handle questions about capital cities
def math_questions():
    # Handle questions about math
print("Select a test topic: animals, capital cities, or math.")
topic = input("Enter 'animal', 'city', or 'math': ")
if topic == 'animal':
    animal_questions()
elif topic == 'city':
    city_questions()
elif topic == 'math':
    math_questions()
else:
    print("Invalid topic. Please try again.")
This approach allows you to easily manage and expand the program by adding more topics and their respective question functions.
0 votes
by (26.4k points)

I executed a full illustration of the True and False test with various inquiries at every point, in addition to approval of the information and collection of results, I trust this can be a genuine model

animals_questions = 'Animals Questions'

capitals_questions = 'Capitals Questions'

math_questions = 'Math Questions'

questions = [animals_questions, capitals_questions, math_questions]

quiz = {animals_questions: [("All lionesses in a pride", True),

                        ("Another animals question", False),

                        ("Last animals question", False)],

        capitals_questions: [("Cairo is the capital city of Egypt", True),

                         ("Another capitals question", True),

                         ("Last capitals question", False)],

        math_questions: [("20 is log 100 for base 1o", False),

                     ("Another math question", True),

                     ("Last math question", False)]

}

result = {"Correct": 0, "Incorrect": 0}

def get_quiz_choice():

    while True:

        try:

            quiz_number = int(raw_input(

                'Choose the quiz you like\n1 for {}\n2 for {}\n3 for {}\nYour choice:'.format(animals_questions,

                                                                                          capitals_questions,

                                                                                          math_questions)))

        except ValueError:

            print "Not a number, please try again\n"

        else:

            if 0 >= quiz_number or quiz_number > len(quiz):

                print "Invalid value, please try again\n"

            else:

                return quiz_number

def get_answer(question, correct_answer):

    while True:

        try:

            print "Q: {}".format(question)

            answer = int(raw_input("1 for True\n0 for False\nYour answer: "))

        except ValueError:

            print "Not a number, please try again\n"

        else:

            if answer is not 0 and answer is not 1:

                print "Invalid value, please try again\n"

            elif bool(answer) is correct_answer:

                result["Correct"] += 1

                return True

            else:

                result["Incorrect"] += 1

                return False

choice = get_quiz_choice()

quiz_name = questions[choice - 1]

print "\nYou chose the {}\n".format(quiz_name)

quiz_questions = quiz[quiz_name]

for q in (quiz_questions):

    print "Your answer is: {}\n".format(str(get_answer(q[0], q[1])))

Output:

/usr/bin/python /Users/arefaey/workspace/playground/python/Quiz/quiz.py

Choose the quiz you like

1 for Animals Questions

2 for Capitals Questions

3 for Math Questions

Your choice: 2

You chose the Capitals Questions

Q: Cairo is the capital city of Egypt

1 for True

0 for False

Your answer: 1

Your answer is: True

Q: Another capitals question

1 for True

0 for False

Your answer: 1

Your answer is: True

Q: Last capitals question

1 for True

0 for False

Your answer: 1

Your answer is: False

You have finished the quiz with score: {'Incorrect': 1, 'Correct': 2}

Are you looking for a good python tutorial? Join the python course fast and gain more knowledge in python.

Watch this video tutorial on how to become a professional in python

0 votes
by (25.7k points)
For creating a program where the user can choose the type of test and then answer questions based on that topic, you can use a modular approach. Here's a suggestion on how you can structure your code:

Define separate functions for each topic with their respective sets of questions. For example, animal_questions(), city_questions(), and math_questions().

Inside each function, you can display the questions related to that topic and handle the user's answers accordingly. You can use loops to iterate through the questions and provide feedback to the user.

In the main part of your code, prompt the user to select the topic by printing the available options and accepting their input.

Based on the user's input, call the corresponding function to start the test. For example, if the user selects 'animal', you can call animal_questions().

Here's an example code snippet to illustrate this approach:

def animal_questions():

    # Display and handle animal-related questions

def city_questions():

    # Display and handle city-related questions

def math_questions():

    # Display and handle math-related questions

print("Do you want to answer questions on animals, capital cities, or math?")

print("Type 'animal', 'city', or 'math'")

topic = input()

if topic == 'animal':

    animal_questions()

elif topic == 'city':

    city_questions()

elif topic == 'math':

    math_questions()

else:

    print("Invalid topic choice. Please try again.")

By organizing your code this way, you can keep each topic's questions and logic separate, making it easier to manage and expand your program in the future.
0 votes
by (15.4k points)

To create a program where users can choose a test topic and answer questions based on their selection, you can use a modular approach. Here's a suggested structure:

  1. Define separate functions for each topic, such as animal_questions(), city_questions(), and math_questions(). Each function should contain the relevant set of questions and handle user responses.

  2. In the main part of your code, prompt the user to select a topic by displaying the available options and accepting their input.

  3. Based on the user's input, call the corresponding function to initiate the test. For instance, if the user selects 'animal', invoke animal_questions().

Here's a simplified example to illustrate this approach:
def animal_questions():
    # Display and handle questions about animals
def city_questions():
    # Display and handle questions about capital cities
def math_questions():
    # Display and handle questions about math
print("Choose a test topic: animals, capital cities, or math.")
topic = input("Type 'animal', 'city', or 'math': ")
if topic == 'animal':
    animal_questions()
elif topic == 'city':
    city_questions()
elif topic == 'math':
    math_questions()
else:
    print("Invalid topic. Please try again.")
By organizing your code in this manner, you can easily manage and expand the program by adding more topics and their respective question functions.

Related questions

0 votes
1 answer
asked Feb 7, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 30, 2020 in Python by Rekha (2.2k points)
0 votes
1 answer

Browse Categories

...