我的问题是为什么我的程序不能在python 3中运行,它似乎是在" Speed = dist / time部分中触发的。非常感谢您的帮助。 如果你也可以纠正它会非常有帮助。
Illegal=[]
Legal=[]
Count = 0
DIST = 0
TIME = 0
SPEED = 0
def CalSpeed():
global SPEED
SPEED=DIST/TIME
return SPEED
print (" Welcome to the Speed check calculator by Awiar Nasseri")
print ("\n")
Count=int(input("How many registration numbers are there?: "))
LIMIT=int(input ("Speed limit (M/S): "))
VAR=int(input ("How many (M/S) should be allowed over the limit?: "))
LIMIT=LIMIT+VAR
while Count > 0:
Count=Count-1
REG = input ("Enter Registration number: ")
TIME =int (input("Enter the time that the vehicle was in the zone in seconds (e.g. 1min= 60)"))
DIST = input ("Distance inside the zone (M): ")
SPEED=(DIST/TIME)
if SPEED>LIMIT:
Illegal.append(REG)
elif SPEED<LIMIT:
Legal.append(REG)
print ("Press P to print illegal and legal cars or press nothing to exit: ")
if option=="P":
print (Legal[0])
print (Illegal[0])
print("Thank you for using the program, Goodbye")
sys.exit(0)
这是错误:
Traceback (most recent call last):
File "\\bhs-students\1515787\Desktop\assess.py", line 26, in <module>
SPEED=(DIST/TIME)
TypeError: unsupported operand type(s) for /: 'str' and 'int'
答案 0 :(得分:2)
将第26行更改为
DIST = int(input ("Distance inside the zone (M): "))
SPEED=(DIST/float(TIME))
答案 1 :(得分:0)
您没有将DIST变量转换为Int,这意味着如果失败,因为它不能将字符串除以int。
答案 2 :(得分:0)
正如错误所示,您不能将字符串除以int。您需要将输入转换为数字类型
DIST = float(input("Distance inside the zone (M): "))