Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops
#1
for i in range(3):
    category = input("Enter a Category: ")
    for j in range(3):
        enter_for_category = input("Enter Something in that category: ")
This is what i'm supposed to do:
Write a program that asks the user for three categories. For each category, ask the user for three things in that category. You should print something for each category that states the category and the three things in that category.

Here is what running your final program should look like:

Enter a category: animals
Enter something in that category: cheetah
Enter something in that category: pelican
Enter something in that category: red panda
animals: cheetah pelican red panda
Enter a category: noises
Enter something in that category: vroom!
Enter something in that category: whirrr!
Enter something in that category: whooosh!
noises: vroom! whirrr! whooosh!
Enter a category: programming languages
Enter something in that category: Python
Enter something in that category: Java
Enter something in that category: C++
programming languages: Python Java C++


This is my results:
Enter a Category: animals
Enter Something in that category: bird
Enter Something in that category: cat
Enter Something in that category: dog
Enter a Category: noise

Fixed
Reply
#2
You need to store the answers from the user in a structure of data (so store somehow your enter_for_category in a data structure)

To put it clear, you can solve your exercise with the following:
for i in range(3):
    category = input("Enter a Category: ")
    # The next lines are a cry to suspend...
    a = input("Enter Something in that category: ")
    b = input("Enter Something in that category: ")
    c = input("Enter Something in that category: ")

    # Report the results:
    print(f"{category}: {a} {b} {c}")
So you see what you shall change... you need to replace the "a, b, c" with a data structure that allows you to fill it with a loop. Take a look to the dict and list in the python documentation.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020