我已经为此工作了几个小时,但无法正常工作。这是我的下面的代码。
我需要使用布尔变量编写代码。如果饥饿为True,并且coworkers_going为True或Bred_lunch为False,则代码应显示True。
Here is my code:
hungry = True
coworkers_going = False
brought_lunch = False
print(((hungry) and coworkers_going) or brought_lunch)
由于“ coworkers_going为True或Bred_lunch为False”一起被考虑,并且or
运算符的优先级低于and
运算符,因此应使用括号将两个表达式分组,以便or
运算符为在and
操作之前进行评估。另外,当表达式为not
时,请使用True
运算符返回False
:
hungry and (coworkers_going or not brought_lunch)
根据我的理解,如果要打印True
并同时显示hungry == True
或coworkers_going == True
,则要打印brought_lunch == False
,就像这样:
print((hungry and (coworkers_going or not brought_lunch)))