Python从列表中选择一个名称

时间:2015-12-07 19:20:25

标签: python

有没有办法从列表中选择一个事件并选择它以便添加一个名称?     events = [“100m Dash”]

 menu = int(input("###Welcome to the PE departments Sports Day        
 program\nPlease enter a number to progress through the menu:\n1.Input 
 events and names  of students\n2.Add the winners to a text document\n3.Open  
 the house point tracking###"))

if menu == 1:

menu1 = int(input("Please enter a number to progress through the menu:\n1.Add an event\n2.Add an name"))

if menu1 == 2:
    print events


    name_1 = int(input("Which event do you want to add the name to?:"))
    if name_1 == events:
        name_1_1 = str(input("Please enter a name you would like to enter:"))
        print(name_1_1 ,"has been added to text file: Names and Events")
        file_appender = open("names.txt","w")
        file_appender.write(name_1_1)

1 个答案:

答案 0 :(得分:0)

names = ["steve", "bob", "joe"]
events = {"100m Dash": names}
print(events)
name_1_1 = str(input("Please enter a name you would like to enter:"))
names.append(name_1_1)
events["100m Dash"] = names
print(events)

这是以下输出:

{'100m Dash': ['steve', 'bob', 'joe']}
{'100m Dash': ['steve', 'bob', 'joe', 'robert']}

这使用dictionaries按事件跟踪名称。