-1

For an assignment in a my python class. I have to take the following list data:

animal = [
    ('cat', 'meow', 4),
    ('dog', 'bark', 10),
    ('bird', 'chirp', 0.5),
    ('snake', 'hiss', 3),
    ('cow', 'moo', 250),
    ('lion', 'roar', 500)
]

And create nested loop statements to display the following: [1]: https://i.stack.imgur.com/KpPL4.png

Yunan
  • 11
  • 2
  • 1
    What have you tried so far, and what in particular are you stuck on? StackOverflow is not a homework service. – Green Cloak Guy Jan 05 '21 at 23:30
  • I'm able to get the output but only using a single loop rather than a nesting loop. and I'm not really asking to be given the answers, just to be pointed in the right direction. – Yunan Jan 05 '21 at 23:43
  • 1
    [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – SiKing Jan 05 '21 at 23:53
  • Add the code that you have for the single loop solution. I wouldn't want to use a nested loop, but I have some ideas how you could do it. Pretend that you cannot use indexed structures. – wbg Jan 06 '21 at 01:00

1 Answers1

0

I think it won't help you learn if we give you the complete answer, but here are some things that can help:

  1. Do you know that each of those structures (the array, and the tuples in the array) is an iterable? That means you can loop over them without using indexes, the way one might in another language. Check out that link to get some ideas about how you could nest an iterable for your individual animals inside the iterable for the whole list.

  2. If you're using Python 3.6 or newer, you can use f strings for your formatting, which will give you a tidy way to print the values with flexible spacing, so they all line up the way you'd like. There are good examples of the older format for formatting strings with whitespace padding in this SO question: Python spacing and aligning strings

I think you should be able to figure out your answer based on those two pointers.

Jim J
  • 546
  • 3
  • 11