How do I ask for a set list in Python, ask for a number (x), remove x number of inputs, and re-list those?

Viewed 81

I am new to Python, and I am currently learning about lists. This is the question that I am trying to solve:

Your favourite band is in town, and tickets are selling fast! Alas, you were too late to snag one, so you put your name in the waitlist, in case any extra tickets are released.

Write a program to manage the waitlist for the concert.

Your program should read in a list of the names in the waitlist, and the number of extra tickets released.

Then, it should announce the names of people who score the extra tickets.

Here's an example of how your program should work:

People in line: Dave, Lin, Toni, Markhela, Ravi
Number of extra tickets: 3
Tickets released for: Dave, Lin, Toni

Note: The names are separated by a comma and a space (', ').

If there are no more tickets released, your program should work like this:

People in line: Mali, Micha, Mary, Monica
Number of extra tickets: 0
Fully Booked!

This band is so popular that there will always be at least as many people as extra tickets. You won't have to worry about index errors.

I have tried the following, but it always prints the entire list, not just a subset.

ppl = []
sep = ', '
ppl_in_line = input('People in line: ')
ppl.append(ppl_in_line)
x = int(input('Number of extra tickets: '))
if x == 0:
    print('Fully Booked!')
else:    
    y = ppl[:x]
    print('Tickets released for: ' + (sep.join(y)))
2 Answers

ppl_in_line is a string. So when you append to ppl, you are appending a single string.

To enter a separated list of ppl on a single line do this:

ppl_in_line = input('People in line: ').split(sep)

You forgot to split your people in line into multiple elements:

ppl_in_line = input('People in line: ')
ppl = ppl_in_line.split(sep)

This is assuming that your input for People in line: is something like

Dave, Lin, Toni, Markhela, Ravi

If you want to use ppl.append, you have to mention them name by name in a loop:

while True:
    ppl_in_line = input('People in line: ')
    if not ppl_in_line:
        break
    ppl.append(ppl_in_line)

You can enter the names like

Dave
Lin
Toni
Markhela
Ravi

An empty input will finish the list.

Related