这是我的源代码:
destinations={"",}
destinations.discard("")
flightterminals={"Terminal 1":[],"Terminal 2":[], "Terminal 3":[]}
proceed="yes"
count=0
flightnumber=int(input("How many flights do you want to add: "))
for i in range(flightnumber):
count+=1
print("Where is plane {} headed?".format(count))
temp=input("")+"1"
destinations.add(temp[:-1])
while ((temp in flightterminals["Terminal 1"]) or (temp in flightterminals["Terminal 2"]) or (temp in flightterminals["Terminal 3"])):
temp=temp[:-1]+str(int(temp[-1])+1)
print(type(temp))
print(temp)
flightterminals["Terminal {}".format((count-1)%3+1)]+=temp
print(flightterminals["Terminal 1"])
这里是终端:
Flight Terminal Manager 5600
How many flights do you want to add: 1
Where is plane 1 headed?
x
<class 'str'>
x1
['x', '1']
似乎倒数第三行flightterminals["Terminal {}".format((count-1)%3+1)]+=temp
向我的字典中的集合添加了两个字符串,尽管temp
在前一行中显示只是一个字符串。我做错了什么?
字典中的元素是列表。在您所谈论的线路中,您使用+=
算子。因此,temp
被认为是一个列表,它的元素被推到字典中相应列表的末尾。你必须使用append()
:flightterminals["Terminal {}".format((count-1)%3+1)].append(temp)