简体   繁体   中英

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.

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

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): "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM