1

我试图让我的程序限制用户可以输入的内容。它不断从下面的代码中返回“预期的缩进块”错误。

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)
4

3 回答 3

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)
于 2012-10-16T04:10:03.807 回答
1

Python 使用意图而不是{}or 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()):
于 2012-10-16T01:43:51.037 回答
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)

对于它的价值,我已经为可读性做了一些风格上的改变。变量名中的一些额外间距、空行和下划线使一切看起来更容易。

于 2012-10-16T01:43:18.913 回答