2016-02-11 58 views
0

我使用python 3空閒,它沒有突出顯示任何東西來告訴我什麼語法錯誤。提到它在第九行,但我看不到它。當運行我的應用程序時,保持得到'SyntaxError:意外的EOF,而解析'當我運行我的應用程序,不知道爲什麼

下面的代碼,它是一個「速度檢查」

import time#python module with time related functions 

file = open('speeders.txt', 'r') 
speeders = file.read() 
print (speeders) #prints out list of speeding cars 

reg_plate = int(input("Please enter the car's registration plate"))#registration plate 
speed_limit = int(input("Please enter your speed limit in mph"))#assigns speed limit 
input("Press enter when the car passes the first sensor")#assign values to the end and start time variables 
start_time = time.time() 
input("Press enter when the car passes the second sensor") 
end_time = time.time() 
distance = float(input("Enter the distance between the two sensors in metres")) #assigns a value to distance 
time_taken = end_time - start_time #works out the time it took the car to travel the length of the road 

AverageSpeed = distance/time_taken #works out the average speed of the car 
print ("The average speed of the car is", AverageSpeed, "m/s") #prints out the average speed of the car in m/s 
AverageSpeedMPH = (AverageSpeed * 2.23694) #converts to mph 
print ("That's", AverageSpeedMPH, "in mph") #prints out the speed in mph 

if AverageSpeedMPH > speed_limit: #prints out whether car is speeding, adds to txt file 
    print (reg_plate, "is speeding") 
    file = open("speeders.txt", "a") 
    file.write(reg_plate + ",") 
    file.close() 
else: 
    print (reg_plate, "is not speeding, be on your merry way") #prints out if not speeding 

學校項目下面是當應用程序運行

Please enter the car's registration plate5 
Please enter your speed limit in mph5 
Press enter when the car passes the first sensor 

Traceback (most recent call last): 
    File "C:\Users\Szymon\Google Drive\Computing\Actual CA work\app2.py", line 9, in <module> 
    input("Press enter when the car passes the first sensor")#lines 3-7 assign values to the end and start time variables 
    File "<string>", line 0 

    ^
SyntaxError: unexpected EOF while parsing 
+0

檢查你正在使用哪個Python版本,並確保它是你應該在的那個版本。 – user2357112

+0

您是否輸入文字,然後按下回車鍵?或者是從其他地方複製粘貼的「mph5」? – dshapiro

回答

0

看起來你正在使用Python2,但仍顯示什麼使用input()。嘗試切換到Python3,或使用raw_input()

0

使用raw_input()而不是 '輸入()'

If you use input, then the data you type is is interpreted as a Python Expression which means that you end up with gawd knows what type of object in your target variable, and a heck of a wide range of exceptions that can be generated. So you should NOT use input unless you're putting something in for temporary testing, to be used only by someone who knows a bit about Python expressions.

在這裏尋找更多。 More Info

相關問題