Python TypeError:/:“function”和“function”的操作数类型不受支持
嗨,我不明白我在做什么,请帮忙!平均速度似乎有错误Python TypeError:/:“function”和“function”的操作数类型不受支持,python,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",
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'
哦,孩子,你的代码离工作还很远
不能使用相同的名称定义函数和变量;距离和时间是函数,因为您稍后定义了它们
改为使用小写变量名,从函数返回结果并将结果存储在变量中
请注意,打印和返回不是一回事;将输出写入终端或控制台后,print始终返回None
您还使用了==相等测试,其中应该使用=赋值。平等测试是一种表达;它返回的结果为True或False,它不会影响与之进行比较的名称
您没有正确测试数字输入。有关如何正确处理用户响应(包括转换为其他类型)的信息,请参阅。表达式Distance==int==True永远不会为True,因为即使Distance包含用户输入,字符串值也永远不会等于int类型,int类型也永远不会等于True
次要的一点:您正在导入re,但从未使用过它。您可以安全地删除导入重新行
因此,这里有太多的问题;如果我把它们都修好,我会帮你做作业。我建议你把你的问题分解成更小的程序。一次做一件事。先把它做好。然后转到下一个。重新阅读以查看可能遗漏的内容。错误行遗漏:AverageSpeed=打印您的速度是,距离/时间,单位为米/秒
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()