Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python学习-if、elif和else语句-某些条件为true但未执行(打印),为什么?_Python - Fatal编程技术网

Python学习-if、elif和else语句-某些条件为true但未执行(打印),为什么?

Python学习-if、elif和else语句-某些条件为true但未执行(打印),为什么?,python,Python,问题: 当第二条elif语句为真时,程序不执行第二条elif语句…为什么 比如, 输入: 输入ppl#1=100,ppl#2=31,ppl#3=30,它不会打印出该语句(请参见第二条elif语句) 预期产出: 最大的是:100,最小的是:30'###这一行来自第二条elif语句 ppl1=int(input('\r\n person #1: pls enter your age: ')) ppl2=int(input('\r\n person #2: pls enter your age: ')

问题: 当第二条elif语句为真时,程序不执行第二条elif语句…为什么

比如,

输入: 输入ppl#1=100,ppl#2=31,ppl#3=30,它不会打印出该语句(请参见第二条elif语句)

预期产出: 最大的是:100,最小的是:30'###这一行来自第二条elif语句

ppl1=int(input('\r\n person #1: pls enter your age: '))
ppl2=int(input('\r\n person #2: pls enter your age: '))
ppl3=int(input('\r\n person #3: pls enter your age: '))

if (ppl3 > ppl1) and (ppl3 > ppl2): 
  if (ppl1 > ppl2):
    print(f'\r\n the oldest is:{ppl3} and the youngest is: {ppl2}')

elif ppl1 > ppl3 and ppl1> ppl2:
  if(ppl3 > ppl2):
    print(f'\r\n the oldest is:{ppl1} and the youngest is: {ppl2}')

elif (ppl1 > ppl3) and (ppl1 > ppl2): 
  if ppl2 > ppl3:
    print(f'\r\n the oldest is: {ppl1} and the youngest is: {ppl3}')

elif ppl3 > ppl1 and ppl3 > ppl2: 
  if(ppl2 > ppl1):
    print(f'\r\n the oldest is: {ppl3} and the youngest is: {ppl1}')

elif ppl2 > ppl1 and ppl2 > ppl3: 
  if(ppl3 > ppl1):
    print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl1}')

elif ppl2 > ppl1 and ppl2 > ppl3: 
  if ppl1 > ppl3:
    print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl3}')

else:
  print(f'\r\n The three people may be with the same age')

该问题是由嵌套的
if
块引起的:

elif ppl1>ppl3和ppl1>ppl2:
if(ppl3>ppl2):#嵌套if块
打印(f'\r\n最大的是:{ppl1},最小的是:{ppl2})
嵌套的if块不受“父”块的elif/else语句的影响。这基本上意味着一旦你进入这里:

elif ppl1 > ppl3 and ppl1> ppl2:
无论内部代码做什么,都不再考虑其他elif/else语句

问题的解决方案是避免嵌套if块,而是使用
语句链接条件:

elif ppl1 > ppl3 and ppl1 > ppl2 and ppl3 > ppl2:
  print(f'\r\n the oldest is: {ppl1} and the youngest is: {ppl2}')
或者,您可以将else添加到内部if块:

elif ppl1>ppl3和ppl1>ppl2:
如果ppl3>ppl2:
打印(f'\r\n最大的是:{ppl1},最小的是:{ppl2})
其他:
打印(…)

(对所有块执行相同操作)

如果
块:

elif ppl1>ppl3和ppl1>ppl2:
if(ppl3>ppl2):#嵌套if块
打印(f'\r\n最大的是:{ppl1},最小的是:{ppl2})
嵌套的if块不受“父”块的elif/else语句的影响。这基本上意味着一旦你进入这里:

elif ppl1 > ppl3 and ppl1> ppl2:
无论内部代码做什么,都不再考虑其他elif/else语句

问题的解决方案是避免嵌套if块,而是使用
语句链接条件:

elif ppl1 > ppl3 and ppl1 > ppl2 and ppl3 > ppl2:
  print(f'\r\n the oldest is: {ppl1} and the youngest is: {ppl2}')
或者,您可以将else添加到内部if块:

elif ppl1>ppl3和ppl1>ppl2:
如果ppl3>ppl2:
打印(f'\r\n最大的是:{ppl1},最小的是:{ppl2})
其他:
打印(…)

(对所有块执行相同操作)

在多个
if
elif
语句中具有相同的条件。如果条件为true,则不会执行以下
elif
语句,因此只使用第一个语句

您需要嵌套您的条件,而不是两次测试相同的东西

if (ppl3 > ppl1) and (ppl3 > ppl2): 
    if (ppl1 > ppl2):
        print(f'\r\n the oldest is:{ppl3} and the youngest is: {ppl2}')
    elif (ppl2 > ppl1):
        print(f'\r\n the oldest is: {ppl3} and the youngest is: {ppl1}')
    else:
        print(f'\r\n the oldest is: {ppl3} and {ppl1} and {ppl2} are the same age')

elif ppl1 > ppl3 and ppl1> ppl2:
    if(ppl3 > ppl2):
        print(f'\r\n the oldest is:{ppl1} and the youngest is: {ppl2}')
    elif ppl2 > ppl3:
        print(f'\r\n the oldest is: {ppl1} and the youngest is: {ppl3}')
    else:
        print(f'\r\n the oldest is: {ppl1} and {ppl2} and {ppl3} are the same age')

elif ppl2 > ppl1 and ppl2 > ppl3: 
    if(ppl3 > ppl1):
        print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl1}')
    elif ppl1 > ppl3:
        print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl3}')
    else:
        print(f'\r\n the oldest is: {ppl2} and {ppl1} and {ppl3} are the same age')

else:
    print(f'\r\n The three people may be with the same age')

在多个
if
elif
语句中都有相同的条件。如果条件为true,则不会执行以下
elif
语句,因此只使用第一个语句

您需要嵌套您的条件,而不是两次测试相同的东西

if (ppl3 > ppl1) and (ppl3 > ppl2): 
    if (ppl1 > ppl2):
        print(f'\r\n the oldest is:{ppl3} and the youngest is: {ppl2}')
    elif (ppl2 > ppl1):
        print(f'\r\n the oldest is: {ppl3} and the youngest is: {ppl1}')
    else:
        print(f'\r\n the oldest is: {ppl3} and {ppl1} and {ppl2} are the same age')

elif ppl1 > ppl3 and ppl1> ppl2:
    if(ppl3 > ppl2):
        print(f'\r\n the oldest is:{ppl1} and the youngest is: {ppl2}')
    elif ppl2 > ppl3:
        print(f'\r\n the oldest is: {ppl1} and the youngest is: {ppl3}')
    else:
        print(f'\r\n the oldest is: {ppl1} and {ppl2} and {ppl3} are the same age')

elif ppl2 > ppl1 and ppl2 > ppl3: 
    if(ppl3 > ppl1):
        print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl1}')
    elif ppl1 > ppl3:
        print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl3}')
    else:
        print(f'\r\n the oldest is: {ppl2} and {ppl1} and {ppl3} are the same age')

else:
    print(f'\r\n The three people may be with the same age')

这是因为当其中一条elif语句为true时,代码没有理由查看其他条件语句。必须改进嵌套块的使用

例如:

elif ppl1 > ppl3 and ppl1> ppl2:
  if(ppl3 > ppl2):
    print(f'\r\n the oldest is:{ppl1} and the youngest is: {ppl2}')

elif (ppl1 > ppl3) and (ppl1 > ppl2): 
  if ppl2 > ppl3:
    print(f'\r\n the oldest is: {ppl1} and the youngest is: {ppl3}')
这里,当您输入100,31,30-
ppl1>pp3和pp1>pp2
时为true,这意味着程序进入第一条elif语句并检查if条件。由于if条件为false,因此它存在

ppl1=int(input('\r\n person #1: pls enter your age: '))
ppl2=int(input('\r\n person #2: pls enter your age: '))
ppl3=int(input('\r\n person #3: pls enter your age: '))

if (ppl3 > ppl1) and (ppl3 > ppl2): 
  if (ppl1 > ppl2):
    print(f'\r\n the oldest is:{ppl3} and the youngest is: {ppl2}')
  elif(ppl2 > ppl1):
    print(f'\r\n the oldest is: {ppl3} and the youngest is: {ppl1}')

elif ppl1 > ppl3 and ppl1> ppl2:
  if(ppl3 > ppl2):
    print(f'\r\n the oldest is:{ppl1} and the youngest is: {ppl2}')
  elif (ppl2>ppl3):
        print(f'\r\n the oldest is:{ppl1} and the youngest is: {ppl3}') 

elif ppl2 > ppl1 and ppl2 > ppl3: 
  if(ppl3 > ppl1):
    print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl1}')
  elif(ppl1>ppl3):
    print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl3}')

else:
  print(f'\r\n The three people may be with the same age')

这将为您提供所需的输出。请注意if-elif块是如何使用的。

这是因为当其中一条elif语句为true时,代码没有理由查看其他条件语句。必须改进嵌套块的使用

例如:

elif ppl1 > ppl3 and ppl1> ppl2:
  if(ppl3 > ppl2):
    print(f'\r\n the oldest is:{ppl1} and the youngest is: {ppl2}')

elif (ppl1 > ppl3) and (ppl1 > ppl2): 
  if ppl2 > ppl3:
    print(f'\r\n the oldest is: {ppl1} and the youngest is: {ppl3}')
这里,当您输入100,31,30-
ppl1>pp3和pp1>pp2
时为true,这意味着程序进入第一条elif语句并检查if条件。由于if条件为false,因此它存在

ppl1=int(input('\r\n person #1: pls enter your age: '))
ppl2=int(input('\r\n person #2: pls enter your age: '))
ppl3=int(input('\r\n person #3: pls enter your age: '))

if (ppl3 > ppl1) and (ppl3 > ppl2): 
  if (ppl1 > ppl2):
    print(f'\r\n the oldest is:{ppl3} and the youngest is: {ppl2}')
  elif(ppl2 > ppl1):
    print(f'\r\n the oldest is: {ppl3} and the youngest is: {ppl1}')

elif ppl1 > ppl3 and ppl1> ppl2:
  if(ppl3 > ppl2):
    print(f'\r\n the oldest is:{ppl1} and the youngest is: {ppl2}')
  elif (ppl2>ppl3):
        print(f'\r\n the oldest is:{ppl1} and the youngest is: {ppl3}') 

elif ppl2 > ppl1 and ppl2 > ppl3: 
  if(ppl3 > ppl1):
    print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl1}')
  elif(ppl1>ppl3):
    print(f'\r\n the oldest is: {ppl2} and the youngest is: {ppl3}')

else:
  print(f'\r\n The three people may be with the same age')

这将为您提供所需的输出。记下if-elif块的使用方法。

谢谢大家的提示。。。很高兴我在没有意识到我误解了以下语句的情况下得到了澄清——“if块只能有一个else块。但是它可以有多个elif块。”谢谢大家的提示。。。很高兴我在没有意识到我误解了以下语句的情况下澄清了它——“if块只能有一个else块,但它可以有多个elif块。”