簡體   English   中英

運行我的應用程序時,不斷收到“ SyntaxError:解析時出現意外的EOF”,無法弄清原因

[英]Keep getting a 'SyntaxError: unexpected EOF while parsing' when running my app, can't figure out why

我正在使用python 3空閑,並且沒有突出顯示任何內容來告訴我語法錯誤是什么。 提到它在第九行,盡管我看不到。

這是代碼,這是“速度檢查器”的學校項目

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

看起來您正在使用Python2,但仍在使用input() 嘗試切換到Python3,或改用raw_input()

使用raw_input()代替'input()'

如果使用輸入,則您輸入的數據將被解釋為Python表達式,這意味着您最終以gawd知道目標變量中的對象類型,以及可能生成的大量異常。 因此,除非您將一些東西用於臨時測試,否則您不應該使用輸入,只能由對Python表達式有一定了解的人使用。

在這里查看更多。 更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM