Understanding the logic with conditions using return statement python.

huangapple go评论72阅读模式
英文:

Understanding the logic with conditions using return statement python

问题

I have translated the content you provided:

给定两个整数值,如果其中一个为负数,另一个为正数,则返回True。但如果参数"negative"为True,则仅当两者都为负数时返回True。

所以,当我解决这个问题时,我制定了尽可能简单的逻辑,基于之前的练习。

def pos_neg(a, b, negative):

  if (negative and (a < 0 and b < 0)):
    return True
  if (a < 0 and b > 0) or (a > 0 and b < 0):
    return True
  else:
    return False

但是,即使我在第二行声明"negative == True",这个解决方案也不起作用,给我提供了这个答案表:
输入和输出

我无法弄清楚条件有什么问题,以及这个逻辑与正确答案有何不同:

def pos_neg(a, b, negative):
  if negative:
    return (a < 0 and b < 0)
  else:
    return ((a < 0 and b > 0) or (a > 0 and b < 0))

此外,我想知道何时将逻辑条件放在return语句之后是正确的。

英文:

I have the following exercise from codingbat.com:

Given 2 int values, return True if one is negative and one is positive. Except if the parameter "negative" is True, then return True only if both are negative.

So, when I was solving it, I've formulated a simple logic as possible, based on previously exercises.

def pos_neg(a, b, negative):

  if (negative and (a &lt; 0 and b &lt; 0)):
    return True
  if (a &lt; 0 and b &gt; 0) or (a &gt; 0 and b &lt; 0):
    return True
  else:
    return False

But this solution does not work even if I declare "negative == True" on the second line,
giving me this table of answers:
Inputs and Outputs

I can't figure out what could be wrong with the conditions, and how this logic differs from the correct answer:

def pos_neg(a, b, negative):
  if negative:
    return (a &lt; 0 and b &lt; 0)
  else:
    return ((a &lt; 0 and b &gt; 0) or (a &gt; 0 and b &lt; 0))

Also, I wished to know when it is correct to put the logical condition after the return statement.

答案1

得分: 0

代码部分不需要翻译。以下是翻译好的部分:

"It's simple, your program fails because when your parameter "negative" is True, yes or yes, "a" and "b" must be negative.

But in your case whenever "a" or "b" are negative it will return True, even though "negative" is True.

One way to solve your problem is to simply make sure that "negative" is False when checking that "a" or "b" are negative (This does the same as the answer to the problem, just in a different way).

def pos_neg(a, b, negative):

if (negative and (a < 0 and b < 0)):
return True
if (not negative) and ((a < 0 and b > 0) or (a > 0 and b < 0)):
return True
else:
return False"

英文:

It's simple, your program fails because when your parameter "negative" is True, yes or yes, "a" and "b" must be negative.

But in your case whenever "a" or "b" are negative it will return True, even though "negative" is True.

One way to solve your problem is to simply make sure that "negative" is False when checking that "a" or "b" are negative (This does the same as the answer to the problem, just in a different way).

def pos_neg(a, b, negative):

  if (negative and (a &lt; 0 and b &lt; 0)):
    return True
  if (not negative) and ((a &lt; 0 and b &gt; 0) or (a &gt; 0 and b &lt; 0)):
    return True
  else:
    return False

huangapple
  • 本文由 发表于 2023年1月6日 12:00:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定