Python 应为缩进块年龄=输入(“年龄或类型:”)

Python 应为缩进块年龄=输入(“年龄或类型:”),python,python-3.x,Python,Python 3.x,我已经为我需要做的工作编写了一些代码,但是当我运行代码时,它会说 File "/home/ubuntu/workspace/Odeon/153670.py", line 24 age = input("Age or Type: ") ^ IndentationError: expected an indented block 我想知道是否有人能帮我解决这个问题 下面列出了发生错误的部分代码年龄=输入(“年龄或类型:”) 打印(“请输入您的年龄或类型。”) 尽管如此: 年龄类型=

我已经为我需要做的工作编写了一些代码,但是当我运行代码时,它会说

File "/home/ubuntu/workspace/Odeon/153670.py", line 24
    age = input("Age or Type: ")
    ^
IndentationError: expected an indented block
我想知道是否有人能帮我解决这个问题

下面列出了发生错误的部分代码<代码>年龄=输入(“年龄或类型:”)

打印(“请输入您的年龄或类型。”)
尽管如此:
年龄类型=无
int_计数=0
峰值标志=无
int_年龄=0
总价=0
尽管如此:
**年龄=输入(“年龄或类型:”)**此行将显示错误
如果取消定价列表.keys()。\uuu包含(age.lower()):
年龄\类型=年龄较低()
打破
尝试:
int_年龄=int(年龄)
如果int_年龄<2且int_年龄>130:
打印(“请输入正确的年龄。”)
持续
打破
除:
打印(“请输入正确的年龄或类型”)

缩进,Python不像其他语言那样有括号来表示代码块,而是有缩进

print ("Please Input Your age or type.")

while True:
   age_type = None
   int_count = 0
   peak_flag = None
   int_age = 0
   totalprice = 0
   while True:
       age = input("Age or Type: ")** This line brings the error
       if unpeak_price_list.keys().__contains__(age.lower()):
           age_type = age.lower()
           break
       try:
           int_age = int(age)
           if int_age < 2 and int_age > 130:
               print("Please Input Correct age.")
               continue
           break
       except:
           print("Please Input Correct age or type")
打印(“请输入您的年龄或类型。”)
尽管如此:
年龄类型=无
int_计数=0
峰值标志=无
int_年龄=0
总价=0
尽管如此:
年龄=输入(“年龄或类型:”)**此行将显示错误
如果取消定价列表.keys()。\uuu包含(age.lower()):
年龄\类型=年龄较低()
打破
尝试:
int_年龄=int(年龄)
如果int_年龄<2且int_年龄>130:
打印(“请输入正确的年龄。”)
持续
打破
除:
打印(“请输入正确的年龄或类型”)

Python不像其他解释或编译语言那样使用“{}”来启动或完成函数或流控制结构,它使用“:”和缩进,通常为4个空格,每次设置“:”时,您需要多放4个空格,如果流控制结构中的代码完成,则需要返回4个空格,例如:

if a is '':
    a = "Hello word" # Here we add 4 spaces because you start an if 
    if b == 4:
        a += str(b) # Here we add 4 spaces because you start an if 
    else:           # Go back 4 spaces because the last if was finished
        b += 1
while和其他流量控制结构的情况相同:

while statement_1:
    a += 1
    while statement_2:
        b += 2

如果您已经意识到这一点,并且正在遵循上面所写的内容,那么错误可能是您的文本编辑器使用的是选项卡而不是空格,用于编码的文本编辑器通常会在将来更改空格的选项卡,您可以启用它

这段代码的问题是不同的代码块结构不好,在其他语言中,当您使用循环(例如)开始和结束时,您使用{},在Python中,您必须使用如下选项卡:

while True:
    print('Hello World!')
etc..
...
同样的情况也会发生在所有代码上,您必须使用制表符来构造代码。
:)

这是您的代码的固定版本:

打印(“请输入您的年龄或类型。”)

为True时:
年龄类型=无
int_计数=0
峰值标志=无
int_年龄=0
总价=0
尽管如此:
年龄=输入(“年龄或类型:”)
如果取消定价列表.keys()。\uuu包含(age.lower()):
年龄\类型=年龄较低()
打破
尝试:
int_年龄=int(年龄)
如果int_年龄<2且int_年龄>130:
打印(“请输入正确的年龄。”)
持续
打破
除:
打印(“请输入正确的年龄或类型”)

您需要确保在while循环中缩进if语句和try/except语句。我建议使用更大的缩进,使代码更具可读性

请格式化您的问题。另外,如果在复制/粘贴时忽略了所有缩进(可能是制表符和空格的混合),我们应该如何帮助解决缩进错误。尝试学习最基本的知识。在Python中,缩进用于分隔代码块。在while之后以粗体设置的行应该缩进,位于
while
块中。您将再次遇到此错误。这只是编译器命中的第一个实例。仔细检查您的压痕可能重复的
while True:
    print('Hello World!')
etc..
...
while True:
    age_type = None
    int_count = 0
    peak_flag = None
    int_age = 0
    totalprice = 0
    while True:
        age = input("Age or Type: ")
        if unpeak_price_list.keys().__contains__(age.lower()):
            age_type = age.lower()
            break
    try:
        int_age = int(age)
        if int_age < 2 and int_age > 130:
        print("Please Input Correct age.")
        continue
        break
    except:
        print("Please Input Correct age or type")