我无法打印清单

时间:2015-03-19 10:09:32

标签: python list

def main():
    Details = {"Name": [], "Speed": [], "Time": [], "Distance":[] , "Point": []} #This is the list of data
    print ("How many runners are there")
    amount_of_runners = input(" ") #This is to enter the amount of runners
    try: #try this
        amount_of_runners = int(amount_of_runners) #This is an input
    except: #dose that
        print ("Only use a number") #This is so the program catches and wrong format inputs
        main() #SS
    x = 0 #This is so we can input the runners
    while x != amount_of_runners:
        name = input ("Person name ")
        try: #This makes it a string
            name = str(name)
        except: #dose that
            print ("Use letters")
            main()
        speed = int (input("Persons speed ")) #This is the speed
        try:
           speed = int(speed)
        except:
            print ("Use numbers")
            main()
        time = int (input ("How long they ran for ")) #Asks for input
        try:
            time = int(time)

        except:
            print ("Use numbers")
            main() #Returns to menu
        Details["Name"].append(name)
        Details["Speed"].append(int (speed))
        Details["Time"].append(int (time))
        Details["Distance"].append(speed*time)
        Details["Point"].append(x) #This adds to a specific part of a list

        x+=1 # To keep loop
    y = 0
    while y != amount_of_runners: #This area is what will not work the rest dose work
        for i in Details["Point"]:
            if y in i:
                print ("Name is ",[i[0]]," Speed is " ,[i[1]], " Time is " ,    [i[2]], " Distance is ",[i[3]])
        y+=1  




    #Please help

我需要它来打印订单中的列表 我想它,所以我=一个数字 因此,跑者1是我 它打印名称速度时间和跑步者1的距离 然后转到下一个跑步者,直到完成         main()的

2 个答案:

答案 0 :(得分:0)

这些行不起作用:

           for i in Details["Point"]:
                if y in i:
                    print ("Name is ",[i[0]]," Speed is " ,[i[1]], " Time is " ,    [i[2]], " Distance is ",[i[3]])

Details["Point"]是一个整数列表,for循环遍历列表中的每个元素,因此每个i都是一个整数。您不能对整数进行索引,也不能对整数使用in(因为in检查某些内容是否是序列的成员,而整数不是序列)。

您可能只想做i=Details["Point"]或类似的事情:

           i = Details["Point"]:
           if y in i:
               print ("Name is ",[i[0]]," Speed is " ,[i[1]], " Time is " ,    [i[2]], " Distance is ",[i[3]])

答案 1 :(得分:0)

for i in Details["Point"]:
                if y in i:
                    print ("Name is ",[i[0]]," Speed is " ,[i[1]], " Time is " ,    [i[2]], " Distance is ",[i[3]])
            y+=1  

问题出在这里。 Details["Point"]:您只获得了点(整数)值。但您正尝试使用i打印列表。这是问题

另外我不知道为什么你需要这个if y in i:因为i是整数你会得到错误。

 while y != amount_of_runners: #This area is what will not work the rest dose work
            for i in Details["Point"]:
                    print ("Name is ", Details["Name"][i]," Speed is " , Details["Speed"][i], " Time is " ,   Details["Time"][i], " Distance is ",Details["Distance"][i])
            y+=1

此代码将打印以下结果

o / p

How many runners are there
 2
Person name AA
Persons speed 23
How long they ran for 23
Person name BB
Persons speed 23
How long they ran for 34
Name is  AA  Speed is  23  Time is  23  Distance is  529
Name is  BB  Speed is  23  Time is  34  Distance is  782
Name is  AA  Speed is  23  Time is  23  Distance is  529
Name is  BB  Speed is  23  Time is  34  Distance is  782

你为什么需要而且为了?你想要打印什么o / p?