我需要python来浏览名称列表并打印以下内容: 第一次看到名称时,会显示“欢迎名称”(该名称) 第二次看到名称时,将打印“ welcome back name”。 到目前为止,我所知道的是,如果名称多次出现,它将被附加到第二个列表中。我怎样才能让python在第一次看到名字时打印出欢迎字,然后在第二次看到欢迎字时打印出
?guests=input("What are the names of the guests? ")
guests=guests.split(",")
m=()
for i in guests:
if guests.count(i)>1:
m.append(i)
for i in m:
else:
print ("Welcome, ", i)
答案 0 :(得分:0)
如果我了解您的问题,我认为这就是您要寻找的
guests=input("What are the names of the guests? ")
guests=guests.split(",")
other = []
for person in guests:
if person in other:
print('Welcome back, ', person)
else:
print('Welcome, ', person)
other.append(person)
如果我输入名字:
craig, smith, smith, john, sarah, vicky, jenny, john
输出为:
Welcome, craig
Welcome, smith
Welcome back, smith
Welcome, john
Welcome, sarah
Welcome, vicky
Welcome, jenny
Welcome back, john