0

I'm brand new to coding, taking an Intro to Python class, and I feel like I've been about 50% lost from the beginning. I'm trying to do the homework and I'm stuck. I want my program to do this:

Please enter a bird species or press Q to quit: Enters in Penguin
Penguin is added to the list.
Penguin
Please enter a bird species or press Q to quit: Enters in Eagle
Eagle is added to the list.
Penguin
Eagle
Please enter a bird species or press Q to quit: Enters in Sparrow
Sparrow is added to the list.
Penguin
Eagle
Sparrow
Please enter a bird species or press Q to quit: Enters QEnds program
Please enter a bird species or press Q to quit: Enters Q
Goodbye. Ends program

Like I said, I'm really new to this so if anyone could help me out that would be awesome. I just need it to repeat itself so it keeps asking and adding to the list. Thanks. I also can't figure out how to put my code in here properly so bear with me.

Here's my code:

bird_names = [ ]
answer = input('Please enter a bird species or type Q to quit: ')

if answer in bird_names:
    print(answer, 'is already in the list.')

elif answer in 'Qq':
    print('Goodbye.')

else:    
    bird_names.append(answer)
    print(answer, 'added to the list.')

for i in range(len(bird_names)):
    print(i, bird_names[i])
L3viathan
  • 26,748
  • 2
  • 58
  • 81
  • This is not a duplicate question, at least not of the supposed duplicate linked. – L3viathan Feb 12 '15 at 21:37
  • @L3viathan. It shows how to keep looping like your answer so I don't see how you think it is not a duplicate, the difference is the dupe goes into a lot more detail and covers other topics that will be extremely useful to the OP. – Padraic Cunningham Feb 12 '15 at 21:40
  • Yeah, I tried looking through many others (believe me, I'm quite frustrated at this point) and I couldn't find anything that I really understood as far as my question was concerned. I'm sorry if it seems like a duplicate to some, but believe me, it doesn't to me. :( – Kendall P. Webber Feb 12 '15 at 21:40
  • @KendallP.Webber, you cannot see how a while loop and putting your code in a function that can be called will do what you want? I suggest you have a good look at the dupe. It covers pretty much every possibility that you will need. Also asking for help doing homework is off topic for SO – Padraic Cunningham Feb 12 '15 at 21:42
  • @PadraicCunningham Look, I'm sorry I inconvenienced you. This is part of an entire project, and I'm not using it to *do my homework*. I'm using it to understand what's going on because I'm lost. I know very little and after searching for a long time on how to figure it out, I asked for help, which I don't think is bad. The "duplicate" is extremely complicated because it uses things that I am not familiar with, that I don't know how to do (not yet anyway). The duplicate helps with other issues, but I apologize for being so stuck. – Kendall P. Webber Feb 12 '15 at 21:49
  • @KendallP.Webber. the dupe shows you how to use a while loop which is all you need. The other topics discussed are optional but worth learning. – Padraic Cunningham Feb 12 '15 at 22:16

1 Answers1

0

Using a while loop:

bird_names = []

while True:
    answer = input('Please enter a bird species or type Q to quit: ')

    if answer in bird_names:
        print(answer, 'is already in the list.')
    elif answer in 'Qq':
        print('Goodbye.')
        break
    else:
        bird_names.append(answer)
        print(answer, 'added to the list.')
    for name in bird_names:
        print(name)

Note the much easier for loop at the end. If you need the indices, consider using enumerate()

L3viathan
  • 26,748
  • 2
  • 58
  • 81