-2
這是爲Python中的票據供應商提供的程序。 直到我在time.sleep
函數中添加它才工作正常。 這裏是我的代碼:time.sleep()函數顯然是導致語法無效的原因
import time
spaces = 61
def init():
global spaces
if spaces > 1:
print("Welcome to OCR's car park!")
print("The number of spaces remaining is "+ str(spaces-1) +".")
print("What are the last three letters on your number plate?")
numplate = input()
if len(numplate) != 3:
print("These letters are invalid.")
time.sleep(1)
init()
elif numplate.isalpha():
print("It's £3.45 for a ticket. Enter how much money you want to insert. Type 'cancel' to cancel.")
confirm = input()
if confirm == "cancel":
print("Payment cancelled.")
time.sleep(3)
init()
if int(confirm) < 345:
print("You don't have enough money.")
time.sleep(1)
init()
print("£" +str(confirm)+ " inserted.")
time.sleep(2)
if float(confirm)> 345:
print("Taking change...")
time.sleep(3)
confirm = float(confirm)-345
print("Change due: £" +str(len(confirm[0]))+ "." +str(len(confirm[1]))+str(len(confirm[2]))
time.sleep(1)
print("Printing ticket. Please wait.")
time.sleep(5)
print("Thank you for choosing OCR's car park!")
spaces -= 1
print("")
print("")
print("|-----|")
print("| "+str(numplate.upper())+"|")
print("|-----|")
print("")
print("")
init()
else:
print("These letters are invalid.")
time.sleep(1)
init()
elif spaces < 2:
print("No More Spaces.")
init()
我每次運行它,它會顯示一個彈出說'invalid syntax'
,它始終指向time.sleep(1)
功能。
錯誤在上面的行。上面的打印行上有一個未封閉的支架。它抱怨睡眠線,因爲這條線被解釋爲上述線的延續。我建議你試試像PyCharm或Eclipse這樣的IDE,它會爲你突出顯示這些錯誤。 – Dan
爲了獲得幫助,您應該首先修復縮進 – Tonechas