我是 Python 新手,目前正在学习列表。这就是我想要解决的问题:
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.
我尝试了以下方法,但它总是打印整个列表,而不仅仅是一个子集。
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)))
最佳答案
ppl_in_line 是一个字符串。因此,当您附加到 ppl 时,您将附加一个字符串。
要在一行中输入单独的 ppl 列表,请执行以下操作:
ppl_in_line = input('排队的人:').split(sep)
关于python - 如何在 Python 中请求一组列表,请求一个数字 (x),删除 x 个输入,然后重新列出这些输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60784096/