[英]What is wrong with this python program? (Traceback error)
my question is why isn't my program working in python 3, It appears to be triggered in the "Speed=dist/time section. Your help would be appreciated. If you could also correct it would be very helpful. 我的问题是为什么我的程序不能在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)
Here is the error: 这是错误:
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'
Change line no 26 to 将第26行更改为
DIST = int(input ("Distance inside the zone (M): "))
SPEED=(DIST/float(TIME))
您没有将DIST变量转换为Int,这意味着如果失败,因为它无法将字符串除以int。
Just as the error is indicating, you cannot divide a string by an int. 正如错误所指示的那样,您不能将字符串除以整数。 You need to convert your input to a numeric type
您需要将输入转换为数字类型
DIST = float(input("Distance inside the zone (M): "))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.