该数据库存储您服务的真实用户列表,包括几个测试用户。程序在第一行获取所有用户的完整列表,在第二行获取列表中测试用户的索引。使用 for 循环,仅在一行上打印真实的用户名。
输入示例 1:
Alice, Bob, Charlie, Dave, Eve
1, 3
示例输出 1:
Alice Charlie Eve
输入示例 2:
Alice, Bob, Charlie
0, 2
示例输出 2:
Bob
我决定这样
full_list=input().split(", ") # full_list=['Alice', 'Bob', 'Charlie', 'Dave', 'Eve']
indexes=list(map(int, input().split(", "))) # indexes=[1, 3]
real_list=[]
for i, text in enumerate(full_list):
if indexes[0]==i:
continue
if indexes[1]==i:
continue
real_list.append(text)
print(*real_list)
在沙箱中一切都很好,答案是正确的,但在教育课程中该解决方案不起作用。问题
Failed test #3 of 3. Runtime error
Error:
Traceback (most recent call last):
File "jailed_code", line 8, in <module>
if indexes[1]==i:
IndexError: list index out of range
我尝试使用 while
full_list=input().split(", ")
a,b=list(map(int, input().split(", ")))
i=0
names_of_real_users=[]
while len(full_list)>i:
names_of_real_users=full_list[i]
i=i+1
if names_of_real_users==full_list[a]:
continue
if names_of_real_users==full_list[b]:
continue
print(names_of_real_users, end=" ")
这次的错误是:
Failed test #3 of 3. Runtime error
Error:
Traceback (most recent call last):
File "jailed_code", line 3, in <module>
a,b=list(map(int, input().split(", ")))
ValueError: not enough values to unpack (expected 2, got 1)
如果有3个或1个索引怎么办?
谢谢@MBo 看到我的错误
几乎所有事情都做对了。剩下的就是将索引收集到一个集合中并过滤列表: