Python条件if statemn

2024-04-26 12:10:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的初学者,在学习python的过程中,我在执行if语句时出错

有人能告诉我代码中遗漏了什么吗

File "Demo_class2_Conditions.py", line 8
print(X)
^
IndentationError: expected an indented block

X=int(input('Enter the value of X:'))
Y=int(input('Enter the value of Y:'))
Z=int(input('Enter the value of Z:'))
if X > Y:
        if X > Y:
        print(X)
        else:
        print(Y)
    else:
if Y > Z:
        if Y > Z:
        print(Y)
        else:
        print(Z)

Tags: ofthe代码inputifvalue过程语句
1条回答
网友
1楼 · 发布于 2024-04-26 12:10:07

在条件之后,应始终缩进属于块的语句(2或4个空格)

您的示例可以正确缩进,如下所示:

# first evaluation starts with no indentation
if X > Y: 
  print(X) # single statement (block 1) if previous condition satisfied
else:
  # block 2 begins
  print(Y)
  # block 2 ends

# second evaluation starts with no indentation
if Y > Z:
  print(Y) # block 3 has only 1 statement
else:
  print(Z) # single statement is block 4

# everything after evaluating is not indented

相关问题 更多 >