gpt4 book ai didi

python - 当我运行程序时,为什么 python 会出现错误“需要缩进 block ?”

转载 作者:行者123 更新时间:2023-11-30 23:40:07 24 4
gpt4 key购买 nike

我试图让我的程序限制用户可以输入的内容。它不断从下面的代码返回“预期缩进 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 - 当我运行程序时,为什么 python 会出现错误“需要缩进 block ?”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12906406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com