0

无法计算距离?(TypeError: unsupported operand type(s) for /: 'int' and 'str') 建议让 python 识别哪个名称与哪个数字对应?这样我就可以让它打印正确的名字和获胜时间?(字符串?)

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)
4

3 回答 3

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也不会给出想要的输出

于 2013-02-15T08:29:25.587 回答
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."
于 2013-02-15T08:31:05.787 回答
0

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

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

您还应该写distance = ((230./time)*681.1)(注意 230 后面的“.”),以便将浮点数除以某个数字。浮点除法不同于整数除法。

于 2013-02-15T08:33:38.623 回答