Python 编码 bat 预热返回意外的 True
Python coding bat warmup returning unexpected True
正在尝试解决以下问题:
The parameter weekday
is True
if it is a weekday
, and the parameter vacation
is True
if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return True
if we sleep in.
sleep_in(False, False) → True
sleep_in(True, False) → False
sleep_in(False, True) → True
我尝试了运行以下代码来测试解决方案。
weekday = 0
vacation = 5
def sleep_in(weekday, vacation):
if not weekday or vacation:
return True
else:
return False
x = sleep_in(0, 6)
print x
我期待的结果是 False。然而我越来越真实了!有什么想法吗?
所有Python对象都有一个布尔值;数字 0
被认为是假的,每隔一个数字都是真的。
因此 0
为假,6
为真,并且 not 0 or 6
计算为 True
因为 not 0
为真:
>>> not 0
True
请参阅 Python 文档中的 Truth Value Testing:
Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:
[...]
- zero of any numeric type, for example, 0, 0L, 0.0, 0j.
因此,您不需要使用 if
语句;直接 return 表达式结果 :
def sleep_in(weekday, vacation):
return not weekday or vacation
weekday = 0
vacation = 1
def sleep_in(weekday,vacation):
weekday = not weekday
if weekday or vacation: return True
return False
print (sleep_in(weekday,vacation))
基本上您正在寻找第一个值和第二个值的 not
。我想先定义它们更好。输出是正确的。或者单线;
x =lambda weekday,vacation: not weekday or vacation
print (x(0,0))
由于多种原因,您的代码将无法运行。首先,您不需要将工作日和假期定义为任何数字,因为它们已经将这些值传递给您为 true 或 false。此外,您需要做的就是检查 weekday 是否为 != True,假期是否为 false,如果是 return True。否则,您将 return 错误。就这么简单
def sleep_in(weekday, vacation):
if not weekday or vacation:
return True
return False
此外,它是 returning 这个错误,因为在 python 中 0 被认为是假的,6 是真的,所以它认为你试图将它们设置为布尔值,这会覆盖什么你被给了。希望这对您有所帮助!
如果是工作日参数weekday为True,如果我们休假则参数vacation为True。如果不是工作日或我们正在休假,我们会睡 。 Return 正确 如果我们睡懒觉。
加粗的语句本身就是提示。
if not weekday or vacation:
return True
return False
正在尝试解决以下问题:
The parameter
weekday
isTrue
if it is aweekday
, and the parametervacation
isTrue
if we are on vacation. We sleep in if it is not a weekday or we're on vacation. ReturnTrue
if we sleep in.sleep_in(False, False) → True sleep_in(True, False) → False sleep_in(False, True) → True
我尝试了运行以下代码来测试解决方案。
weekday = 0
vacation = 5
def sleep_in(weekday, vacation):
if not weekday or vacation:
return True
else:
return False
x = sleep_in(0, 6)
print x
我期待的结果是 False。然而我越来越真实了!有什么想法吗?
所有Python对象都有一个布尔值;数字 0
被认为是假的,每隔一个数字都是真的。
因此 0
为假,6
为真,并且 not 0 or 6
计算为 True
因为 not 0
为真:
>>> not 0
True
请参阅 Python 文档中的 Truth Value Testing:
Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:
[...]
- zero of any numeric type, for example, 0, 0L, 0.0, 0j.
因此,您不需要使用 if
语句;直接 return 表达式结果 :
def sleep_in(weekday, vacation):
return not weekday or vacation
weekday = 0
vacation = 1
def sleep_in(weekday,vacation):
weekday = not weekday
if weekday or vacation: return True
return False
print (sleep_in(weekday,vacation))
基本上您正在寻找第一个值和第二个值的 not
。我想先定义它们更好。输出是正确的。或者单线;
x =lambda weekday,vacation: not weekday or vacation
print (x(0,0))
由于多种原因,您的代码将无法运行。首先,您不需要将工作日和假期定义为任何数字,因为它们已经将这些值传递给您为 true 或 false。此外,您需要做的就是检查 weekday 是否为 != True,假期是否为 false,如果是 return True。否则,您将 return 错误。就这么简单
def sleep_in(weekday, vacation):
if not weekday or vacation:
return True
return False
此外,它是 returning 这个错误,因为在 python 中 0 被认为是假的,6 是真的,所以它认为你试图将它们设置为布尔值,这会覆盖什么你被给了。希望这对您有所帮助!
如果是工作日参数weekday为True,如果我们休假则参数vacation为True。如果不是工作日或我们正在休假,我们会睡 。 Return 正确 如果我们睡懒觉。
加粗的语句本身就是提示。
if not weekday or vacation:
return True
return False