我正在尝试让我的程序限制用户可以键入的内容。它会从下面的代码中返回“预期的缩进块”错误。
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)
答案 0 :(得分:2)
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)
答案 1 :(得分:1)
Python使用意图代替{}
或begin/end
,例如此行
while len(customerfirstname) <3 or len(customerfirstname)>30 or customerfirstname.isalpha() != True:
后面应加一个缩进块。缩进块可以像单行一样短,通常你应该缩进4个空格,而不是while
除此之外:将该行写为
可能更清晰while not (3 <= len(customerfirstname) <= 30 and customerfirstname.isalpha()):
答案 2 :(得分:0)
确保缩进作为循环一部分的行。这是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)
为了它的价值,我为可读性做了一些风格上的改变。变量名称中的一些额外间距,空白行和下划线使眼睛上的一切变得容易。