这个代码不工作,但我真的觉得它应该b

2024-09-21 09:31:21 发布

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

代码不起作用。问题提示: 松鼠聚会时,喜欢抽雪茄。当雪茄数量在40到60支(含)之间时,松鼠派对是成功的。除非是周末,在这种情况下,雪茄数量没有上限。如果具有给定值的一方成功,则返回True,否则返回False。你知道吗

    def cigar_party(cigars, is_weekend):
        if is_weekend:
            if cigars > 40:
                return True
            else:
                return False
        else:
            if cigars >= 40 and cigars <= 60:
                return True
            else:
          return False

cigar_party(30, False) → False  False   OK  
cigar_party(50, False) → True   True    OK  
cigar_party(70, True) → True    True    OK  
cigar_party(30, True) → False   False   OK  
cigar_party(50, True) → True    True    OK  
cigar_party(60, False) → True   True    OK  
cigar_party(61, False) → False  False   OK  
cigar_party(40, False) → True   True    OK  
cigar_party(39, False) → False  False   OK  
cigar_party(40, True) → True    False   X   
cigar_party(39, True) → False   False   OK  
other tests
OK

Tags: 代码falsetrue数量returnifisparty
1条回答
网友
1楼 · 发布于 2024-09-21 09:31:21

在第一个if语句下,if cigars>;40:cannot=40。下面的代码将正常工作

def cigar_party(cigars, is_weekend):
  if is_weekend:
    if cigars >= 40:
      return True
    else:
      return False
  else:
    if cigars >= 40 and cigars <= 60:
      return True
    else:
      return False

相关问题 更多 >

    热门问题