布尔运算符:使用布尔变量进行分支(python)

2024-04-29 11:46:37 发布

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

我正在做一个家庭作业问题,我很难让它正确地返回最终陈述。在

说明:

Write an expression that prints 'You must be rich!' if the variables young and famous are both True.

代码(我只能更新if语句):

young = True
famous = False
if (young == 'True') and (famous == 'True'):
    print('You must be rich!')
else:
    print('There is always the lottery...')

我最初的想法是上面代码中的组合,但我绝望了,我也尝试了下面所有的组合:

^{pr2}$

结果:

年轻人和名人都是假的
你的输出:总是有彩票。。。

以年轻为真、以名为假进行测试
你的输出:总是有彩票。。。

以young as False和Family as True进行测试
你的输出:总是有彩票。。。

✖ 年轻人和名人都是真的
预期产出:你一定很有钱!
你的输出:总是有彩票。。。在


Tags: andthe代码youfalsetrueifbe
1条回答
网友
1楼 · 发布于 2024-04-29 11:46:37

很明显,你混淆了布尔变量和字符串

young=True #boolean variable

以及

^{pr2}$

这是正确的代码

young = True
famous = False
if young and famous:
    print('You must be rich!')
else:
    print('There is always the lottery...')

我建议你在使用这个之前先复习一下字符串和布尔变量的课程,祝你好运

相关问题 更多 >