TypeError:/:'function'和'function'的不支持的操作数类型

时间:2015-03-04 11:41:39

标签: python

嗨,我不明白我在做什么,请帮忙!平均速度似乎有误!

What is the distance from one of the censor to the other?>>70
How long did it take you to get past the monitored section in seconds?7
Traceback (most recent call last):
File "N:\Controlled Assessment IT\SpeedCheck.py", line 24, in <module>
AverageSpeed()
File "N:\Controlled Assessment IT\SpeedCheck.py", line 23, in AverageSpeed
    AverageSpeed = print("Your speed is",(Distance / TimeTaken), "in metres per second")
TypeError: unsupported operand type(s) for /: 'function' and 'function'

我不知道为什么!基本上我已经在这方面花了这么长时间,这一点让我落后了。我可能花了大约一个多小时才看到代码出了什么问题。

import re
Distance = ""
TimeTaken = ""
AverageSpeed = ""

def Distance():
    Distance == input("What is the distance from one of the censor to the other?>>")
    if Distance == int == True:
        print(Distance)
    if Distance == int == False:
        print("Write only numbers")
Distance()

def TimeTaken():
    TimeTaken == input("How long did it take you to get past the monitored section in seconds?")
    if TimeTaken == int == True:
        print(TimeTaken)
    if TimeTaken == int == False:
        print("Write only numbers")
TimeTaken()

def AverageSpeed():
  AverageSpeed = print("Your speed is",(Distance / TimeTaken), "in metres per second")
AverageSpeed()

1 个答案:

答案 0 :(得分:1)

噢,小伙子,你的代码很远无法工作。

  • 您无法定义具有相同名称的函数和变量; DistanceTimeTaken是函数,因为您稍后定义了这些函数。

    使用小写变量名称,返回函数的结果并将结果存储在变量中。

  • 请注意,打印与返回不同;将输出写入终端或控制台后,print()始终返回None

  • 您还在使用==(相等测试),您应该使用=(赋值)。平等测试是一种表达;它返回一个结果(TrueFalse),它不会影响您要比较的名称。

  • 您没有正确测试数字输入。有关如何正确处理用户响应(包括转换为其他类型)的信息,请参阅Asking the user for input until they give a valid response。表达式Distance == int == True永远不会成立,因为即使Distance包含用户输入,字符串值也永远不会等于int类型,{{1 } type永远不会等于int

  • 小点:您正在导入True但从未使用过它。您可以安全地删除re行。

因此,这里有太多问题;如果我要修理它们,我就会为你做功课。我建议你将问题分解为较小的程序。一次做一件事。让这项工作先行。然后转到下一个。重新阅读Python tutorial以了解您可能错过的其他内容。