类型错误?配对列表?

时间:2013-02-15 08:26:44

标签: python

无法计算距离? (TypeError:/ int'和'str'的不支持的操作数类型)建议让python识别哪个名字与哪个号码一致?那样我可以用赢得的时间打印正确的名字吗? (STR?)

def main():
    print "~*~*~*~*~*~ Timbuktu Archery Contest ~*~*~*~*~*~"
    archerList = [] #list
    timeList = [] #list2
    distanceList = [] #list3
    name = raw_input ("Enter contestants first name: ")
    s = str(name)
    archerList.append(name)
    while name:
        time = raw_input ("Enter time (in milliseconds) for %s: " % s)
        timeList.append(time)
        name = raw_input ("Enter contestants first name: ")
        s = str(name)
        archerList.append(name)
    else:
        print "Slowest archer was " , min(timeList)
        print "Fastest archer was " , max(timeList)
    for time in timeList:
        distance = ((230/time)*681.1)
        distanceList.append(distance)

3 个答案:

答案 0 :(得分:1)

首先不要将time用作变量,它是一个python模块

其次

time = raw_input ("Enter time (in milliseconds) for %s: " % s)此处time是一个字符串

您应该将其转换为int,如timeList.append(int(time)),然后将其用于

distance = ((230/time)*681.1)

如果你不转换min/max也不会提供所需的输出

答案 1 :(得分:1)

time = raw_input ("Enter time (in milliseconds) for %s: " % s)

应该是:

time = int( raw_input ("Enter time (in milliseconds) for %s: " % s) )

甚至更好:

repeat = True
while repeat:
    try:
        time = int(raw_input ("Enter time (in milliseconds) for %s: " % s))
        repeat = False
    except ValueError:
        print "Invalid time."

答案 2 :(得分:0)

正如其他人已经给你回答“关键点”,这里有一些额外的信息。

在计算之前,您必须先将时间转换为int或更确切地说float。 Python是强类型语言。没有变量类型声明,但是,每个变量都有一个类型。

你也应该写distance = ((230./time)*681.1)(注意230后面的“。”),这样你就可以将浮点数除以某个数。浮点除法与整数除法不同。