[英]Python2.7: What is wrong with this program? It runs but the weird traceback error
[英]What is wrong with this python program? (Traceback error)
我的問題是為什么我的程序不能在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'
將第26行更改為
DIST = int(input ("Distance inside the zone (M): "))
SPEED=(DIST/float(TIME))
您沒有將DIST變量轉換為Int,這意味着如果失敗,因為它無法將字符串除以int。
正如錯誤所指示的那樣,您不能將字符串除以整數。 您需要將輸入轉換為數字類型
DIST = float(input("Distance inside the zone (M): "))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.