Forgot Password

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

You must login to ask a question.

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.

Quizzma Latest Articles

5.4.7 Categories CodeHS Answers

The most common answer is:

num_of_categories = 3
num_of_question = 3

# Loop for each category
for i in range(num_of_categories):
    category_name = input("Enter a category: ")
    entries = []  # Initialize an empty list to store entries for each category

    # Loop for each question within a category
    for j in range(num_of_question):
        entry = input(f"Enter something in the category '{category_name}': ")
        entries.append(entry)  # Append each entry to the list

    # Print the category and its entries
    print(category_name + ": " + " ".join(entries))

This code will:

  • Prompt the user to enter a category name for a predetermined number of categories (num_of_categories).
  • For each category, it will then ask the user to enter items (num_of_question times) that belong to that category.
  • After collecting the items for a category, it prints the category name followed by the items separated by spaces.

This approach uses a list (entries) to collect items for each category, simplifying the process of adding and displaying items. The join() method is used to concatenate all items in the list into a single string with spaces in between for printing.

Was this helpful?




Quizzma Team

Quizzma Team

The Quizzma Team is a collective of experienced educators, subject matter experts, and content developers dedicated to providing accurate and high-quality educational resources. With a diverse range of expertise across various subjects, the team collaboratively reviews, creates, and publishes content to aid in learning and self-assessment.
Each piece of content undergoes a rigorous review process to ensure accuracy, relevance, and clarity. The Quizzma Team is committed to fostering a conducive learning environment for individuals and continually strives to provide reliable and valuable educational resources on a wide array of topics. Through collaborative effort and a shared passion for education, the Quizzma Team aims to contribute positively to the broader learning community.

Related Posts

Leave a comment