[英]Why does python, when I run my program comes up with the error 'expected an indented block?
我試圖讓我的程序限制用戶可以鍵入的內容。它繼續從我的代碼中返回“預期的縮進塊”錯誤。
deliverydetails = input("Is your order for delivery?\n Press 1 for delivery. Press 2 for pickup")
if deliverydetails == "1":
## def delivery ():
print ("Order for Delivery")
customerfirstname = " "
while len(customerfirstname) <3 or len(customerfirstname)>30 or customerfirstname.isalpha() != True:
customerfirstname = input("Customer First Name: ** must be 4 characters long + " ")
while len(customersurname) < 3 or len(customersurname) > 30 or customerfirstname.isalpha() != True:
customersurname = input("Customer Surname:" + " ")
customerstreet = input("Street name:" + " ")
customerstreetnumber = input("Street number:" + " ")
customercity = input("City:" + " ")
customersuburb = input("Suburb (If none, leave blank):" + " ")
latestOrder.append(customerfirstname)
latestOrder.append(customersurname)
latestOrder.append(customerstreet)
latestOrder.append(customerstreetnumber)
latestOrder.append(customercity)
latestOrder.append(customersuburb)
Python使用縮進來分組代碼塊。 在while語句之后,你想縮進它應該在while循環中執行的下面的行。
以下是一些可能有用的其他提示: - 使用pylint檢查語法。 它將發現許多錯誤,否則您只能在運行時找到它們。 - 使用空格縮進。 不要使用標簽。 這是PEP 8風格的推薦
以下是代碼的更正版本:
deliverydetails = input("Is your order for delivery?\n Press 1 for delivery. Press 2 for pickup")
if deliverydetails == "1":
## def delivery ():
print ("Order for Delivery")
customerfirstname = " "
customersurname = " "
while len(customerfirstname) <3 or len(customerfirstname)>30 or customerfirstname.isalpha() != True:
customerfirstname = input("Customer First Name: ** must be 4 characters long + " ")
while len(customersurname) < 3 or len(customersurname) > 30 or customerfirstname.isalpha() != True:
customersurname = input("Customer Surname:" + " ")
customerstreet = input("Street name:" + " ")
customerstreetnumber = input("Street number:" + " ")
customercity = input("City:" + " ")
customersuburb = input("Suburb (If none, leave blank):" + " ")
latestOrder.append(customerfirstname)
latestOrder.append(customersurname)
latestOrder.append(customerstreet)
latestOrder.append(customerstreetnumber)
latestOrder.append(customercity)
latestOrder.append(customersuburb)
Python使用意圖而不是{}
或begin/end
,所以例如這一行
while len(customerfirstname) <3 or len(customerfirstname)>30 or customerfirstname.isalpha() != True:
應該跟一個縮進的塊。 縮進塊可以像單行一樣短,通常你應該比while
更多地縮進4個空格
旁白:將該行寫成可能更清楚
while not (3 <= len(customerfirstname) <= 30 and customerfirstname.isalpha()):
確保縮進作為循環一部分的行。 這是Python必須知道你想要循環的部分的唯一方法。
delivery_details = input("Is your order for delivery?\n Press 1 for delivery. Press 2 for pickup")
if delivery_details == "1":
print "Order for Delivery"
customer_first_name = ""
while len(customer_first_name) < 3 or len(customer_first_name) > 30 or not customer_first_name.isalpha():
customer_first_name = input("First name (must be 4 characters long): ")
customer_surname = input("Surname: ")
customer_street = input("Street name: ")
customer_street_number = input("Street number: ")
customer_city = input("City: ")
customer_suburb = input("Suburb (If none, leave blank): ")
latest_order.append(customer_first_name)
latest_order.append(customer_surname)
latest_order.append(customer_street)
latest_order.append(customer_street_number)
latest_order.append(customer_city)
latest_order.append(customer_suburb)
為了它的價值,我為可讀性做了一些風格上的改變。 變量名稱中的一些額外間距,空白行和下划線使眼睛上的一切變得更容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.